What is the main advantage of exceptions over returning error codes?
They also work where no return value exists, such as constructors and operators.
Exception Handling · Exercise 1 of 4
Twenty multiple-choice questions on C++ exception handling: try/catch/throw, the standard exception hierarchy, catch order, rethrowing, stack unwinding, noexcept, constructors, destructors and exception safety. Pick an option to check your answer instantly.
What is the main advantage of exceptions over returning error codes?
They also work where no return value exists, such as constructors and operators.
After a catch block finishes, where does the program continue?
C++ has no "resume" – the rest of the try block is skipped.
Why is catch (const std::exception& e) preferred to catch (std::exception e)?
By value, a derived exception is copied into a plain std::exception.
Why must catch (const std::out_of_range&) come before catch (const std::exception&)?
Compilers often warn about the unreachable handler.
Which handler catches exceptions of any type, including throw 42;?
catch (const std::exception&) only catches classes derived from std::exception.
Inside catch (const std::exception& e), what is the difference between throw; and throw e;?
Outer handlers for the derived type will not match the sliced copy.
What happens to local objects of the functions an exception passes through?
This is why RAII objects make cleanup automatic.
What happens if an exception propagates out of main without being caught?
A top-level try/catch in main lets you log the error and exit cleanly instead.
What happens if an exception escapes a function declared noexcept?
noexcept is a promise; breaking it ends the program.
Why must destructors not let exceptions escape?
Catch and handle errors inside the destructor, or provide an explicit close() that can throw.
Timed online tests with instant scores and explanations.