C++ Exception Handling Interview Questions
try
throw
catch
C++ Exception Handling Fundamentals
Basic Level (15 Questions)
What is exception handling in C++?
Exception handling is a mechanism to handle runtime errors and abnormal conditions in a program. It allows separating error handling code from normal code using try, throw, and catch blocks, making programs more robust and maintainable.
What are the main components of exception handling?
Three main components: try block (contains code that might throw exceptions), throw statement (raises/throws an exception), and catch block (handles/catches thrown exceptions).
What can be thrown as an exception in C++?
Review the tutorial for this topic.
What is the syntax of try-catch block?
Review the tutorial for this topic.
What is stack unwinding in exception handling?
Stack unwinding is the process where the compiler destroys all local objects in scope between the throw point and the catch point. Destructors are called automatically for all constructed objects, ensuring proper cleanup.
What is the catch-all handler?
Review the tutorial for this topic.
What happens if an exception is not caught?
Review the tutorial for this topic.
What is exception specification (throw list)?
Review the tutorial for this topic.
What are standard exception classes in C++?
C++ provides standard exception classes in <stdexcept> header: std::exception (base class), std::runtime_error, std::logic_error, std::out_of_range, std::bad_alloc, etc. User exceptions should derive from std::exception.
How to create custom exception classes?
Review the tutorial for this topic.
What is RAII and how does it relate to exception safety?
Review the tutorial for this topic.
What is noexcept in C++11?
noexcept is a C++11 specifier indicating a function won't throw exceptions. It's a compile-time check. Syntax: void func() noexcept or void func() noexcept(true/false). Violating noexcept leads to std::terminate() being called.
Can constructors and destructors throw exceptions?
Review the tutorial for this topic.
What is rethrowing an exception?
Review the tutorial for this topic.
What are nested try-catch blocks?
Review the tutorial for this topic.
Tricky Level (10 Questions)
What is the order of catch block execution?
Catch blocks are checked in top-to-bottom order. The first matching catch block executes. More specific exceptions should be caught before more general ones. Base class handlers catch derived class exceptions, so derived should come before base.
What is exception safety guarantee?
Review the tutorial for this topic.
Can exceptions be thrown from noexcept functions?
Review the tutorial for this topic.
What is std::exception_ptr?
Review the tutorial for this topic.
When to use exceptions vs error codes?
Use exceptions for: 1) Recoverable errors, 2) Errors in constructors, 3) When error must be handled at higher level, 4) Complex error propagation. Use error codes for: 1) Performance-critical code, 2) C interface compatibility, 3) Expected/common errors.
What is exception safety guarantee basic?
No leaks and invariants hold after exception—object may be in valid but unspecified state.
Should destructors throw?
Destructors should not throw; if they do during stack unwinding, std::terminate is called.
What is `std::nested_exception`?
Allows storing and rethrowing the currently active exception—useful for wrapping errors in thread pools.
Difference between `throw;` and `throw e;`?
Bare throw; rethrows current exception; throw e; throws a copy and may slice polymorphic types.
When to use `noexcept` on functions?
When they never throw—enables optimizations and required for some move operations in containers.
Note: These questions cover C++ exception handling mechanisms including try-catch-throw, exception safety, RAII, and best practices. Exception handling is crucial for writing robust, maintainable C++ applications that handle errors gracefully. This page lists 15 basic and 10 tricky questions—use the tutorial and MCQ links above and below.