cpp/language/try catch

Associates one or more exception handlers (catch-clauses) with a compound statement.

Syntax
where is a sequence of one or more s, which have the following syntax:

@1@ Catch-clause that declares a named formal parameter

@2@ Catch-clause that declares an unnamed parameter

@3@ Catch-all handler, which is activated for any exception

Explanation

 * See for more information about throw-expressions

A try-block is a, and as such, can appear anywhere a statement can appear (that is, as one of the statements in a compound statement, including the function body compound statement). See for the try blocks around function bodies. The following description applies to both try-blocks and s.

The formal parameter of the catch clause ( and or  and ) determines which types of exceptions cause this catch clause to be entered. It cannot be an, type, or pointer to incomplete type (except that pointers to (possibly -qualified)  are allowed). If the type of the formal parameter is array type or function type, it is treated as the corresponding pointer type (similar to a ).

When an exception is thrown by any statement in, the of type  is matched against the types of the formal parameters  of each catch-clause in , in the order in which the catch clauses are listed. The exception is a match if any of the following is true:
 * and are the same type (ignoring top-level cv-qualifiers on )
 * is an lvalue-reference to (possibly cv-qualified)
 * is an unambiguous public base class of
 * is a reference to an unambiguous public base class of
 * is (possibly cv-qualified), and  is a pointer or pointer to member type, and  is also a pointer or pointer to member type that is implicitly convertible to  by one or more of
 * a standard other than one to a private, protected, or ambiguous base class
 * a

The catch-all clause matches exceptions of any type. If present, it has to be the last catch clause in the. Catch-all block may be used to ensure that no uncaught exceptions can possibly escape from a function that offers.

If no matches are found after all catch-clauses were examined, the exception propagation continues to the containing try-block, as described in. If there are no containing try-blocks left, std is executed (in this case, it is implementation-defined whether any stack unwinding occurs at all: throwing an uncaught exception is permitted to terminate the program without invoking any destructors).

When entering a catch clause, if its formal parameter is a base class of the exception type, it is from the base class subobject of the exception object. Otherwise, it is copy-initialized from the exception object (this copy is subject to ).

If the parameter of the catch-clause is a reference type, any changes made to it are reflected in the exception object, and can be observed by another handler if the exception is rethrown with. If the parameter is not a reference, any changes made to it are local and its lifetime ends when the handler exits.

A or  statement shall not be used to transfer control into a try block or into a handler.

Other than by throwing or rethrowing the exception, the catch-clause after a regular try block (not ) may be exited with a, , , , or by reaching the end of its. In any case, this destroys the exception object (unless an instance of std exists that refers to it).

Keywords
, ,