0
0
C++programming~5 mins

Multiple catch blocks in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of multiple catch blocks in C++?
Multiple catch blocks allow a program to handle different types of exceptions separately, providing specific responses based on the exception type.
Click to reveal answer
beginner
How does C++ decide which catch block to execute when an exception is thrown?
C++ checks the type of the thrown exception and matches it with the first compatible catch block in order. It executes the first matching catch block it finds.
Click to reveal answer
intermediate
Can you have a catch block that catches all exceptions regardless of type? How?
Yes, by using a catch block with ellipsis syntax: catch(...). This block catches any exception not caught by previous catch blocks.
Click to reveal answer
intermediate
What happens if no catch block matches the thrown exception?
If no catch block matches, the program calls std::terminate() and usually ends abruptly, unless the exception is caught elsewhere.
Click to reveal answer
beginner
Write a simple example of multiple catch blocks handling different exception types.
Example:<br><pre>try {
  throw 10;
} catch (int e) {
  std::cout << "Caught int: " << e << std::endl;
} catch (const std::exception& e) {
  std::cout << "Caught std::exception: " << e.what() << std::endl;
} catch (...) {
  std::cout << "Caught unknown exception" << std::endl;
}</pre>
Click to reveal answer
What keyword is used to handle exceptions in C++?
Acatch
Btry
Cthrow
Dexcept
Which catch block will handle an exception of type std::runtime_error if multiple catch blocks exist?
ANone, it will be ignored
BThe last catch block
CThe catch block with ellipsis <code>catch(...)</code>
DThe first catch block matching <code>std::runtime_error</code> or its base classes
What does catch(...) do?
ACatches all exceptions not caught by previous catch blocks
BCatches only exceptions of type ellipsis
CIs invalid syntax
DCatches exceptions of type <code>std::exception</code>
If no catch block matches the thrown exception, what happens?
AException is ignored and program continues
BProgram calls <code>std::terminate()</code> and ends
CException is automatically handled by the OS
DProgram retries the try block
Can multiple catch blocks be used to handle different exception types in the same try block?
ANo, only one catch block is allowed per try
BYes, but only if exceptions are of the same type
CYes, each catch block handles a specific exception type
DNo, catch blocks must be nested
Explain how multiple catch blocks work in C++ and why they are useful.
Think about how you might want to respond differently to different problems.
You got /5 concepts.
    Write a short code snippet using multiple catch blocks to handle an integer exception and a standard exception.
    Start with try, throw an int, then catch int and std::exception separately.
    You got /4 concepts.