Challenge - 5 Problems
Try–catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of try–catch with integer exception
What is the output of this C++ code?
C++
#include <iostream> int main() { try { throw 5; } catch (int e) { std::cout << "Caught integer: " << e << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
The throw statement throws an integer, which is caught by the catch block.
✗ Incorrect
The try block throws an integer 5. The catch block catches an int and prints it.
❓ Predict Output
intermediate2:00remaining
Output when no exception is thrown
What will this C++ program output?
C++
#include <iostream> int main() { try { std::cout << "Inside try block" << std::endl; } catch (...) { std::cout << "Caught exception" << std::endl; } std::cout << "After try-catch" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
No exception is thrown, so catch block is skipped.
✗ Incorrect
Since no exception is thrown, the catch block is not executed. Both print statements run.
❓ Predict Output
advanced2:00remaining
Output with multiple catch blocks
What is the output of this C++ code?
C++
#include <iostream> int main() { try { throw 'a'; } catch (int e) { std::cout << "Caught int: " << e << std::endl; } catch (char e) { std::cout << "Caught char: " << e << std::endl; } catch (...) { std::cout << "Caught unknown exception" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
The thrown type is char, so the matching catch block runs.
✗ Incorrect
The throw statement throws a char 'a'. The catch block for char catches it and prints it.
❓ Predict Output
advanced2:00remaining
Exception not caught by specific catch block
What happens when this C++ code runs?
C++
#include <iostream> int main() { try { throw 3.14; } catch (int e) { std::cout << "Caught int: " << e << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
The thrown type is double but catch expects int.
✗ Incorrect
The thrown double 3.14 is not caught by catch(int), so the exception is uncaught and program terminates.
🧠 Conceptual
expert3:00remaining
Behavior of nested try–catch blocks
Consider this C++ code with nested try–catch blocks. What is the output?
C++
#include <iostream> int main() { try { try { throw 10; } catch (double e) { std::cout << "Inner catch double" << std::endl; } } catch (int e) { std::cout << "Outer catch int: " << e << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
The inner catch does not catch int, so exception propagates to outer catch.
✗ Incorrect
The inner try throws int 10, inner catch expects double so it does not catch. Exception goes to outer catch which catches int and prints it.