Challenge - 5 Problems
Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of multiple catch blocks with integer exception
What is the output of this C++ code when an integer exception is thrown?
C++
#include <iostream> using namespace std; int main() { try { throw 10; } catch (double e) { cout << "Caught double: " << e << endl; } catch (int e) { cout << "Caught int: " << e << endl; } catch (...) { cout << "Caught unknown exception" << endl; } return 0; }
Attempts:
2 left
π‘ Hint
Remember that catch blocks are checked in order and the first matching type handles the exception.
β Incorrect
The thrown exception is an int (10). The catch block for double does not match, so it is skipped. The catch block for int matches and handles the exception, printing "Caught int: 10".
β Predict Output
intermediate2:00remaining
Output when throwing a string literal with multiple catch blocks
What will this C++ program output when it throws a string literal exception?
C++
#include <iostream> using namespace std; int main() { try { throw "error"; } catch (int e) { cout << "Caught int: " << e << endl; } catch (const char* e) { cout << "Caught const char*: " << e << endl; } catch (...) { cout << "Caught unknown exception" << endl; } return 0; }
Attempts:
2 left
π‘ Hint
Check which catch block matches the thrown type exactly.
β Incorrect
The thrown exception is a string literal, which is of type const char*. The catch block for int does not match, so the second catch block catches the exception and prints "Caught const char*: error".
π§ Debug
advanced2:00remaining
Identify the error in multiple catch blocks
What error will this C++ code produce when compiled?
C++
#include <iostream> using namespace std; int main() { try { throw 5.5; } catch (int e) { cout << "Caught int: " << e << endl; } catch (double e) { cout << "Caught double: " << e << endl; } catch (double e) { cout << "Caught another double: " << e << endl; } return 0; }
Attempts:
2 left
π‘ Hint
Check if catch blocks have unique exception types.
β Incorrect
The code has two catch blocks for the same type (double), which is not allowed and causes a compilation error.
π§ Conceptual
advanced2:00remaining
Behavior of catch(...) block in multiple catch blocks
Consider the following C++ code snippet. What will be the output if an exception of type std::string is thrown?
C++
#include <iostream> #include <string> using namespace std; int main() { try { throw string("error"); } catch (int e) { cout << "Caught int: " << e << endl; } catch (...) { cout << "Caught unknown exception" << endl; } return 0; }
Attempts:
2 left
π‘ Hint
The catch(...) block catches any exception not caught by previous catch blocks.
β Incorrect
The thrown exception is std::string, which does not match the int catch block. The catch(...) block catches all other exceptions, so it prints "Caught unknown exception".
β Predict Output
expert3:00remaining
Output of nested try-catch with multiple catch blocks
What is the output of this C++ program with nested try-catch blocks and multiple catch handlers?
C++
#include <iostream> using namespace std; int main() { try { try { throw 'a'; } catch (int e) { cout << "Inner caught int: " << e << endl; } catch (...) { cout << "Inner caught unknown" << endl; throw; } } catch (char e) { cout << "Outer caught char: " << e << endl; } catch (...) { cout << "Outer caught unknown" << endl; } return 0; }
Attempts:
2 left
π‘ Hint
Look at how the inner catch rethrows the exception and how the outer catch handles it.
β Incorrect
The inner try throws a char 'a'. The inner catch for int does not match, so the catch(...) block catches it, prints "Inner caught unknown", then rethrows. The outer catch for char matches and prints "Outer caught char: a".