[except.special] (original) (raw)
14 Exception handling [except]
14.6 Special functions [except.special]
14.6.1 General [except.special.general]
The function std::terminate ([except.terminate]) is used by the exception handling mechanism for coping with errors related to the exception handling mechanism itself.
The function std::uncaught_exceptions ([uncaught.exceptions]) reports how many exceptions are uncaught in the current thread.
The function std::current_exception ([propagation]) and the classstd::nested_exception ([except.nested]) can be used by a program to capture the currently handled exception.
14.6.2 The std::terminate function [except.terminate]
Some errors in a program cannot be recovered from, such as when an exception is not handled or a std::thread object is destroyed while its thread function is still executing.
[Note 1:
These situations are:
- when the exception handling mechanism, after completing the initialization of the exception object but before activation of a handler for the exception ([except.throw]), calls a function that exits via an exception, or
- when the exception handling mechanism cannot find a handler for a thrown exception ([except.handle]), or
- when the search for a handlerexits the function body of a function with a non-throwing exception specification, including when a contract-violation handler invoked from an evaluation of a function contract assertion ([basic.contract.eval]) associated with the function exits via an exception, or
- when the destruction of an object during stack unwindingterminates by throwing an exception, or
- when initialization of a non-block variable with static or thread storage duration ([basic.start.dynamic]) exits via an exception, or
- when destruction of an object with static or thread storage duration exits via an exception ([basic.start.term]), or
- when execution of a function registered withstd::atexit or std::at_quick_exitexits via an exception ([support.start.term]), or
- when athrow-expression ([expr.throw]) with no operand attempts to rethrow an exception and no exception is being handled ([except.throw]), or
- when the function std::nested_exception::rethrow_nested is called for an object that has captured no exception ([except.nested]), or
- when execution of the initial function of a thread exits via an exception ([thread.thread.constr]), or
- for a parallel algorithm whose ExecutionPolicy specifies such behavior ([execpol.seq], [execpol.par], [execpol.parunseq]), when execution of an element access function ([algorithms.parallel.defns]) of the parallel algorithm exits via an exception ([algorithms.parallel.exceptions]), or
- when the destructor or the move assignment operator is invoked on an object of type std::thread that refers to a joinable thread ([thread.thread.destr], [thread.thread.assign]), or
- when a call to a wait(), wait_until(), or wait_for()function on a condition variable ([thread.condition.condvar], [thread.condition.condvarany]) fails to meet a postcondition, or
- when a callback invocation exits via an exception when requesting stop on a std::stop_source or a std::inplace_stop_source ([stopsource.mem], [stopsource.inplace.mem]), or in the constructor ofstd::stop_callback orstd::inplace_stop_callback ([stopcallback.cons], [stopcallback.inplace.cons]) when a callback invocation exits via an exception, or
- when a run_loop object is destroyed that is still in the running state ([exec.run.loop]), or
- when unhandled_stopped is called on a with_awaitable_senders<T> object ([exec.with.awaitable.senders]) whose continuation is not a handle to a coroutine whose promise type has an unhandled_stopped member function.
— _end note_]
In the situation where no matching handler is found, it isimplementation-defined whether or not the stack is unwound before std::terminate is invoked.
In the situation where the search for a handler ([except.handle]) exits the function body of a function with a non-throwing exception specification ([except.spec]), it isimplementation-defined whether the stack is unwound, unwound partially, or not unwound at all before the function std::terminate is invoked.
In all other situations, the stack shall not be unwound before the function std::terminateis invoked.
An implementation is not permitted to finish stack unwinding prematurely based on a determination that the unwind process will eventually cause an invocation of the functionstd::terminate.