cpp/language/exceptions

Exception handling provides a way of transferring control and information from some point in the execution of a program to a handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack).

An exception can be thrown by a, , , , allocation function, and any of the standard library functions that are specified to throw exceptions to signal certain error conditions (e.g. std, std, etc).

In order for an exception to be caught, the throw-expression has to be inside a or inside a function called from a try-block, and there has to be a  that matches the type of the exception object.

When declaring a function, the following specification(s) may be provided to limit the types of the exceptions a function may throw:

Errors that arise during exception handling are handled by std.

Usage
While throw-expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to std), its intended usage is error handling.

Error handling
Throwing an exception is used to signal errors from functions, where "errors" are typically limited to only the following  :
 * 1) Failures to meet the postconditions, such as failing to produce a valid return value object.
 * 2) Failures to meet the preconditions of another function that must be called.
 * 3) (for non-private member functions) Failures to (re)establish a class invariant.

In particular, this implies that the failures of constructors (see also RAII) and most operators should be reported by throwing exceptions.

In addition, so-called wide contract functions use exceptions to indicate unacceptable inputs, for example, std has no preconditions, but throws an exception to indicate index out of range.

Exception safety
After the error condition is reported by a function, additional guarantees may be provided with regards to the state of the program. The following four levels of exception guarantee are generally recognized , which are strict supersets of each other:


 * 1) Nothrow (or nofail) exception guarantee — the function never throws exceptions. Nothrow (errors are reported by other means or concealed) is expected of s and other functions that may be called during stack unwinding.  Nofail (the function always succeeds) is expected of swaps, s, and other functions used by those that provide strong exception guarantee.
 * 2) Strong exception guarantee — If the function throws an exception, the state of the program is rolled back to the state just before the function call (for example, std).
 * 3) Basic exception guarantee — If the function throws an exception, the program is in a valid state. No resources are leaked, and all objects' invariants are intact.
 * 4) No exception guarantee — If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred.

Generic components may, in addition, offer exception-neutral guarantee: if an exception is thrown from a template parameter (e.g. from the function object of std or from the constructor of  in std), it is propagated, unchanged, to the caller.

Exception objects
While objects of any complete type and cv pointers to void may be thrown as exception objects, all standard library functions throw anonymous temporary objects by value, and the types of those objects are derived (directly or indirectly) from std. User-defined exceptions usually follow this pattern.

To avoid unnecessary copying of the exception object and object slicing, the best practice for catch clauses is to catch by reference.