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.