ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
catch clause is an exception handler that immediately follows a try block or a function-try-block. It executes when an exception of a matching type is thrown during the execution of that associated block. It intercepts the exception object, halts the stack unwinding process for that specific exception, and provides a scoped block to resolve the error state or delegate it further up the call stack.
Syntax and Handler Ordering
Atry block must be followed by one or more catch clauses. The C++ runtime evaluates catch clauses strictly in the sequential order they are declared. Hierarchical ordering is critical: handlers for derived classes must appear before handlers for their respective base classes. If a base class handler precedes a derived class handler, the base class handler intercepts all derived exceptions, rendering the derived handler unreachable.
The ellipsis syntax catch (...) acts as a universal fallback, matching any exception type regardless of its origin or type signature. Because there is no type declaration, the exception object cannot be bound to an identifier. The catch-all handler must always be the final clause in a try-catch sequence.
Type Matching Rules
Acatch clause matches the thrown exception if any of the following conditions are met:
- Exact Match: The handler type is the exact same type as the exception object, ignoring top-level
constandvolatile(cv-qualifiers). - Base Class Match: The handler type is an unambiguous, public base class of the exception object’s type (caught via reference or value). The C++ runtime exception matcher does not consider the access control context at the
throwpoint; private or protected base classes will never match. - Pointer and Qualification Conversions: The handler type is a pointer, and the thrown exception is a pointer that can be implicitly converted to the handler’s type via standard pointer conversions (e.g., derived pointer to public base pointer, or any non-const pointer to
void*) or qualification conversions (e.g., throwing anint*and catching it via aconst int*handler).
int to double) are not performed during exception matching.
Binding Mechanisms
The parameter declaration in thecatch clause dictates how the exception object is bound to the handler:
- By Reference (
catch (const T& e)orcatch (T& e)): Binds directly to the thrown object. This preserves the dynamic type of the exception and prevents object slicing when catching derived exceptions via a base class reference. - By Value (
catch (T e)): Invokes the copy constructor to create a local copy of the exception object. If a derived exception is caught by a base class value, object slicing occurs, and the derived-specific data and overridden methods are lost. - By Pointer (
catch (T* e)): Matches exceptions thrown as pointers, arrays, or functions (which decay to pointers during the throw expression), as well asstd::nullptr_t(which implicitly converts to a pointer). - Anonymous (
catch (T)orcatch (const T&)): Matches the type, but omits the identifier. The exception is caught, but the object itself cannot be inspected or manipulated within the block. - Rvalue References (Forbidden): Catching by rvalue reference (e.g.,
catch (T&& e)) is explicitly forbidden by the C++ standard. Attempting to do so makes the program ill-formed and will result in a compilation error.
Re-throwing Exceptions
Within acatch block, the exception currently being handled can be propagated further up the call stack using the throw keyword without an operand. Using throw; re-throws the exact original exception object, preserving its dynamic type. Conversely, using throw e; throws a new exception based on the static type of e within the catch block, which causes object slicing if e is a base class reference to a derived exception.
Function-Try-Blocks
A function-try-block associates a sequence ofcatch clauses with the entire body of a function, including the constructor initializer list. This is a critical mechanism, as it is the only way to catch exceptions thrown during the initialization of base classes or non-static data members in a constructor.
When an exception is caught in a function-try-block of a constructor or destructor, the exception is implicitly re-thrown at the end of the catch handler.
Master C++ with Deep Grasping Methodology!Learn More





