General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
try block | ||||
Throwing exceptions | ||||
Handling exceptions | ||||
Exception specification | ||||
noexcept specification(C++11) | ||||
dynamic specification(until C++17*) | ||||
noexcept operator(C++11) |
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).
Evaluating athrow expression will throw an exception. Exceptions can also be thrown inother contexts.
In order for an exception to be caught, thethrow expression has to be inside atry block, and thetry block has to contain ahandler 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:
(until C++17) |
(since C++11) |
Errors that arise during exception handling are handled bystd::terminate andstd::unexpected(until C++17).
Contents |
Whilethrow expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar tostd::longjmp), its intended usage is error handling.
Throwing an exception is used to signal errors from functions, where "errors" are typically limited to only the following[1][2][3]:
In particular, this implies that the failures of constructors (see alsoRAII) and most operators should be reported by throwing exceptions.
In addition, so-calledwide contract functions use exceptions to indicate unacceptable inputs, for example,std::basic_string::at has no preconditions, but throws an exception to indicate index out of range.
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[4][5][6], which are strict supersets of each other:
noexcept
by default.(since C++11) Nofail (the function always succeeds) is expected of swaps,move constructors, and other functions used by those that provide strong exception guarantee.Generic components may, in addition, offerexception-neutral guarantee: if an exception is thrown from a template parameter (e.g. from theCompare
function object ofstd::sort or from the constructor ofT
instd::make_shared), it is propagated, unchanged, to the caller.
While objects of any complete type and cv pointers tovoid may be thrown as exception objects, all standard library functions throw unnamed objects by value, and the types of those objects are derived (directly or indirectly) fromstd::exception. User-defined exceptions usually follow this pattern.[7][8][9]
To avoid unnecessary copying of the exception object and object slicing, the best practice for handlers is to catch by reference.[10][11][12][13]
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_constexpr_exceptions | 202411L | (C++26) | constexpr exceptions |
|