C++ Error Handling Practice (Interactive)
C++ — Error Handling Practice
15 auto-graded MCQ questions covering exceptions, try/catch blocks, noexcept, std::error_code, custom exception hierarchies, RAII-based error handling, and stack unwinding. Select an answer, submit, and review the explanation.
Exceptions and try/catch
noexcept
std::error_code and error handling strategies
Custom Exceptions
RAII and Exception Safety
Stack Unwinding
Common Mistakes
Throwing exceptions from destructors during stack unwinding: Destructors are implicitly noexcept in C++11+. If an exception is already propagating (stack unwinding in progress) and a destructor throws, std::terminate() is called immediately. This means destructors must either swallow exceptions or use error codes — never let exceptions escape.
Catching by value instead of by reference: Writing catch (std::runtime_error e) copies the exception object, potentially slicing derived classes and losing information. Always catch by const reference: catch (const std::runtime_error& e). This preserves the full exception hierarchy and avoids unnecessary copies.
Using catch(…) as the first handler: The ellipsis catch catch(...) matches any exception type. Placing it before typed handlers makes those handlers unreachable dead code. Always put catch(...) last as a catch-all fallback, and consider rethrowing or logging within it.