cpp/language/throw

Signals an erroneous condition and executes an error handler.

Explanation

 * See for more information about try and catch (exception handler) blocks

@1@ First, the exception object from


 * The copy may be subject to


 * then transfers control to the with the matching type for which the compound statement or member initializer list that follows the keyword  was most recently entered and not exited by this thread of execution.

@2@ Rethrows the currently handled exception. Abandons the execution of the current catch block and passes control to the next matching exception handler (but not to another catch clause after the same try block: its compound-statement is considered to have been 'exited'), reusing the existing exception object: no new objects are made. This form is only allowed when an exception is presently being handled (it calls std if used otherwise). The catch clause associated with a must exit via rethrowing if used on a constructor.

See std for the handling of errors that arise during exception handling.

The exception object
The exception object is a temporary object in unspecified storage that is constructed by the expression.

The type of the exception object is the static type of with top-level  removed. Array and function types are adjusted to pointer and pointer to function types, respectively. If the type of the exception object would be an, an type, or pointer to incomplete type other than pointer to (cv-qualified) void, the throw-expression is a compile-time error. If the type of is a class type, its copy constructor and destructor must be accessible even if  takes place.

Unlike other temporary objects, the exception object is considered to be an lvalue argument when initializing the catch clause parameters, so it can be caught by lvalue reference, modified, and rethrown.

The exception object persists until the last catch clause exits other than by rethrowing (if not by rethrowing, it is destroyed immediately after the destruction of the catch clause's parameter), or until the last std that references this object is destroyed (in which case the exception object is destroyed just before the destructor of std returns.

Stack unwinding
Once the exception object is constructed, the control flow works backwards (up the call stack) until it reaches the start of a, at which point the parameters of all associated blocks are compared, in order of appearance, with the type of the exception object to find a match (see  for details on this process). If no match is found, the control flow continues to unwind the stack until the next block, and so on. If a match is found, the control flow jumps to the matching block.

As the control flow moves up the call stack, destructors are invoked for all objects with that are constructed, but not yet destroyed, since the corresponding try-block was entered, in reverse order of completion of their constructors. If an exception is thrown from a destructor of a local variable or of a temporary used in a statement, the destructor for the object returned from the function is also invoked.

If an exception is thrown from a constructor or (rare) from a destructor of an object (regardless of the object's storage duration), destructors are called for all fully-constructed non-static non-variant members and base classes, in reverse order of completion of their constructors. Variant members of are only destroyed in the case of unwinding from constructor, and if the active member changed between initialization and destruction, the behavior is undefined.

If the exception is thrown from a constructor that is invoked by a, the matching deallocation function is called, if available.

This process is called stack unwinding.

If any function that is called directly by the stack unwinding mechanism, after initialization of the exception object and before the start of the exception handler, exits with an exception, std is called. Such functions include s of objects with automatic storage duration whose scopes are exited, and the copy constructor of the exception object that is called (if not ) to initialize catch-by-value arguments.

If an exception is thrown and not caught, including exceptions that escape the initial function of std, the main function, and the constructor or destructor of any static or thread-local objects, then std is called. It is implementation-defined whether any stack unwinding takes place for uncaught exceptions.