Exception Handling Terminology Reference

This reference provides an overview of exception handling terminology you'll encounter in this chapter. Use it as a quick lookup guide.

Exception Basics

Term Definition Example
Exception Object thrown to signal error or unusual condition throw std::runtime_error{"Error"};
throw Statement Raises exception to be caught by handler throw 42;, throw MyException{};
try Block Code that might throw exceptions try { riskyOperation(); }
catch Block Handler for exceptions of specific type catch (const std::exception& e) { }
Exception Handler Code that processes caught exception Code inside catch block
Uncaught Exception Exception with no matching handler Calls std::terminate()

Exception Types

Term Definition Example
Standard Exception Exception from C++ standard library std::exception and derived classes
std::exception Base class for standard exceptions throw std::exception{};
std::runtime_error Exception for runtime errors throw std::runtime_error{"File not found"};
std::logic_error Exception for logic errors throw std::logic_error{"Invalid argument"};
Custom Exception User-defined exception class class MyException : public std::exception { };
Exception Object Instance of exception class being thrown Object passed to catch block

Exception Flow Control

Term Definition Example
Stack Unwinding Destroying objects when exception propagates Destructors called in reverse order
Exception Propagation Exception moving up call stack From throw to catch
Rethrowing Throwing caught exception again catch { throw; }
Catch-all Handler Handler catching any exception type catch (...) { }
Exception Safety Guarantees about program state during exceptions No-throw, strong, basic, or no guarantee
Termination Program ending due to uncaught exception Calls std::terminate()

Exception Safety Levels

Term Definition Example
No-throw Guarantee Function never throws exceptions Marked noexcept
Strong Guarantee Operation succeeds or state unchanged Commit-or-rollback semantics
Basic Guarantee No resource leaks, object remains valid State may change but valid
No Guarantee No promises about exception safety May leak resources
noexcept Specifier Declares function won't throw void func() noexcept;
noexcept Operator Tests if expression can throw noexcept(expr) returns bool

Standard Exception Hierarchy

Term Definition Example
std::logic_error Base for errors detectable before runtime Invalid argument, out of range
std::invalid_argument Argument not in expected range throw std::invalid_argument{"Negative size"};
std::out_of_range Access beyond valid range throw std::out_of_range{"Index too large"};
std::runtime_error Base for errors only detectable at runtime File errors, overflow
std::overflow_error Arithmetic overflow occurred throw std::overflow_error{"Value too large"};
std::underflow_error Arithmetic underflow occurred throw std::underflow_error{"Value too small"};

Exception Specifications

Term Definition Example
noexcept Function guaranteed not to throw void swap(T& a, T& b) noexcept;
noexcept(expression) Conditional noexcept based on expression noexcept(noexcept(T(std::move(other))))
Dynamic Exception Spec Deprecated way to list exceptions (removed C++17) void func() throw(int); (don't use)
throw() Old way to specify no exceptions Use noexcept instead

Resource Management

Term Definition Example
RAII Resource Acquisition Is Initialization Objects manage resources via destructors
Destructor Always called during stack unwinding Releases resources automatically
Smart Pointer Prevents leaks during exceptions std::unique_ptr, std::shared_ptr
Lock Guard RAII wrapper for mutex std::lock_guard<std::mutex> lock{mutex};
Scope Guard Object performing action on scope exit Custom RAII class
Exception-safe Code Code that doesn't leak resources Uses RAII, proper exception handling

Exception Catching Strategies

Term Definition Example
Catch by Reference Preferred way to catch exceptions catch (const std::exception& e)
Catch by Value Copies exception, may cause slicing catch (MyException e) (avoid)
Catch by Pointer Catching pointer to exception Rarely used, ownership unclear
Multiple Catch Blocks Different handlers for different types Most derived to least derived order
Catch Order Order matters, most specific first Base class catch must come last

Advanced Exception Concepts

Term Definition Example
Exception Translation Converting one exception to another Catch and throw different exception
Function try Block try-catch for entire function including initializers Constructor initialization list
Current Exception Getting currently handled exception std::current_exception()
Exception Ptr Type-erased exception object std::exception_ptr
Rethrow Exception Rethrowing previously caught exception std::rethrow_exception(eptr);
Nested Exception Exception thrown while handling another std::throw_with_nested()

Exception-related Functions

Term Definition Example
std::terminate Called for uncaught exception Calls terminate handler
std::set_terminate Sets custom terminate handler std::set_terminate(myHandler);
std::uncaught_exceptions Number of uncaught exceptions Used in destructors
std::make_exception_ptr Creates exception_ptr without throwing auto eptr = std::make_exception_ptr(MyException{});
what() Virtual method returning error message e.what() returns const char*

Best Practices

Term Definition Example
Exception for Errors Only Don't use exceptions for control flow Use for exceptional situations
what() Method Provide descriptive error message Override in custom exceptions
Catch by const Reference Avoid object slicing and copying catch (const std::exception& e)
RAII Everywhere Use RAII to ensure cleanup Smart pointers, lock guards
Constructor Exception Throwing from constructor is safe Object not created if exception thrown
Destructor noexcept Destructors should never throw Marked noexcept by default