cpp/language/except spec

Lists the exceptions that a function might directly or indirectly throw.

Syntax
@1@ Explicit dynamic exception specification.

An explicit dynamic exception specification shall appear only on a function declarator for a function type, pointer to function type, reference to function type, or pointer to member function type that is the top-level type of a declaration or definition, or on such a type appearing as a parameter or return type in a function declarator.

Explanation
If a function is declared with type listed in its dynamic exception specification, the function may throw exceptions of that type or a type derived from it.

, pointers or references to incomplete types other than cv are not allowed in the exception specification. Array and function types, if used, are adjusted to corresponding pointer types, top level cv-qualifications are also dropped. .

A dynamic exception specification whose set of adjusted types is empty is non-throwing. A function with a non-throwing dynamic exception specification does not allow any exceptions.

A dynamic exception specification is not considered part of a function’s type.

If the function throws an exception of the type not listed in its exception specification, the function std is called. The default function calls std, but it may be replaced by a user-provided function (via std) which may call std or throw an exception. If the exception thrown from std is accepted by the exception specification, stack unwinding continues as usual. If it isn't, but std is allowed by the exception specification, std is thrown. Otherwise, std is called.

Potential exceptions
Each function, pointer to function , and pointer to member function has a set of potential exceptions, which consists of types that might be thrown. Set of all types indicates that any exception may be thrown. This set is defined as follows: @1@ If the declaration of, , or uses a dynamic exception specification, the set consists of the types listed in that specification.

@3@ Otherwise, the set is the set of all types.

Note: for implicitly-declared special member functions (constructors, assignment operators, and destructors), the set of potential exceptions is a combination of the sets of the potential exceptions of everything they would call: constructors/assignment operators/destructors of non-variant non-static data members, direct bases, and, where appropriate, virtual bases (including default argument expressions, as always).

Each expression has a set of potential exceptions. The set is empty if is a, otherwise, it is the union of the sets of potential exceptions of all immediate subexpressions of  (including ), combined with another set that depends on the form of , as follows: @1@ If is a function call expression, let  denote the function, function pointer, or pointer to member function that is that is called, then
 * if the declaration of uses a dynamic exception specification, the set of potential exceptions of  is added to the set;


 * otherwise, the set is the set of all types.

@2@ If calls a function implicitly (it's an operator expression and the operator is overloaded, it is a  and the allocation function is overloaded, or it is a full expression and the destructor of a temporary is called), then the set is the set of that function. @3@ If is a, the set is the exception that would be initialized by its operand, or the set of all types for the re-throwing throw-expression (with no operand). @4@ If is a  to a reference to a polymorphic type, the set consists of std. @5@ If is a  applied to a dereferenced pointer to a polymorphic type, the set consists of std.

All implicitly-declared member functions have exception specifications, selected as follows:
 * If the set of potential exceptions is the set of all types, the implicit exception specification.
 * Otherwise, If the set of potential exceptions is not empty, the implicit exception specification lists every type from the set.
 * Otherwise, the implicit exception specification is.