Challenge - 5 Problems
Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of nested try-catch blocks
What is the output of this C++ code when executed?
C++
#include <iostream> int main() { try { try { throw 10; } catch (int e) { std::cout << "Inner catch: " << e << std::endl; throw; } } catch (int e) { std::cout << "Outer catch: " << e << std::endl; } return 0; }
Attempts:
2 left
π‘ Hint
Remember that rethrowing an exception passes it to the next matching catch block outside.
β Incorrect
The inner try throws 10, caught by inner catch which prints and rethrows. The outer catch then catches and prints again.
β Predict Output
intermediate2:00remaining
Exception caught by base class catch block
What will be the output of this C++ program?
C++
#include <iostream> #include <exception> class MyException : public std::exception {}; int main() { try { throw MyException(); } catch (std::exception& e) { std::cout << "Caught std::exception" << std::endl; } catch (...) { std::cout << "Caught unknown exception" << std::endl; } return 0; }
Attempts:
2 left
π‘ Hint
MyException inherits from std::exception, so it matches the first catch.
β Incorrect
Since MyException is derived from std::exception, the first catch block handles it.
β Predict Output
advanced2:00remaining
Exception propagation with function calls
What is the output when this C++ program runs?
C++
#include <iostream> void funcB() { throw "Error in funcB"; } void funcA() { try { funcB(); } catch (const char* msg) { std::cout << "Caught in funcA: " << msg << std::endl; throw; } } int main() { try { funcA(); } catch (const char* msg) { std::cout << "Caught in main: " << msg << std::endl; } return 0; }
Attempts:
2 left
π‘ Hint
Exception thrown in funcB is caught and rethrown in funcA, then caught in main.
β Incorrect
funcB throws, funcA catches and prints, then rethrows. main catches and prints again.
β Predict Output
advanced2:00remaining
Exception handling with multiple catch blocks
What output does this program produce?
C++
#include <iostream> int main() { try { throw 3.14; } catch (int e) { std::cout << "Caught int: " << e << std::endl; } catch (double e) { std::cout << "Caught double: " << e << std::endl; } catch (...) { std::cout << "Caught unknown" << std::endl; } return 0; }
Attempts:
2 left
π‘ Hint
The thrown value is a double, so the matching catch block handles it.
β Incorrect
The throw is a double, so the catch(double) block runs and prints the value.
π§ Conceptual
expert2:00remaining
Exception handling order and program termination
Consider this C++ code snippet. What happens when an exception is thrown but not caught anywhere?
C++
#include <iostream> #include <stdexcept> void func() { throw std::runtime_error("Error"); } int main() { func(); std::cout << "End of main" << std::endl; return 0; }
Attempts:
2 left
π‘ Hint
If no catch block handles the exception, the program ends abruptly.
β Incorrect
Since no catch block handles the thrown exception, the program terminates immediately without printing "End of main".