Files
2025-10-25 03:02:53 +03:00

173 lines
8.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[except.special]
# 14 Exception handling [[except]](./#except)
## 14.6 Special functions [except.special]
### [14.6.1](#general) General [[except.special.general]](except.special.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L1017)
The function std::terminate ([[except.terminate]](#except.terminate "14.6.2The std::terminate function"))
is used by the exception
handling mechanism for coping with errors related to the exception handling
mechanism itself[.](#general-1.sentence-1)
The function std::uncaught_exceptions ([[uncaught.exceptions]](uncaught.exceptions "17.9.6uncaught_­exceptions"))
reports how many exceptions are uncaught in the current thread[.](#general-1.sentence-2)
The function std::current_exception ([[propagation]](propagation "17.9.7Exception propagation")) and the classstd::nested_exception ([[except.nested]](except.nested "17.9.8nested_­exception")) can be used by a program to
capture the currently handled exception[.](#general-1.sentence-3)
### [14.6.2](#except.terminate) The std::terminate function [[except.terminate]](except.terminate)
[1](#except.terminate-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L1030)
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[.](#except.terminate-1.sentence-1)
In such cases,
the function std::terminate ([[exception.terminate]](exception.terminate "17.9.5Abnormal termination")) is invoked[.](#except.terminate-1.sentence-2)
[*Note [1](#except.terminate-note-1)*:
These situations are:
- [(1.1)](#except.terminate-1.1)
when the exception handling mechanism, after completing
the initialization of the exception object
but before
activation of a handler for the exception ([[except.throw]](except.throw "14.2Throwing an exception")),
calls a function that exits
via an exception, or
- [(1.2)](#except.terminate-1.2)
when the exception handling mechanism cannot find a handler for a thrown exception ([[except.handle]](except.handle "14.4Handling an exception")), or
- [(1.3)](#except.terminate-1.3)
when the search for a [handler](except.handle "14.4Handling an exception[except.handle]") exits the function body of a function
with a non-throwing [exception specification](except.spec "14.5Exception specifications[except.spec]"),
including when a contract-violation handler
invoked from an evaluation of
a function contract assertion ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation")) associated with the function
exits via an exception,
or
- [(1.4)](#except.terminate-1.4)
when the destruction of an object during [stack unwinding](except.ctor#def:stack_unwinding "14.3Stack unwinding[except.ctor]") terminates by throwing an exception, or
- [(1.5)](#except.terminate-1.5)
when initialization of a non-block
variable with static or thread storage duration ([[basic.start.dynamic]](basic.start.dynamic "6.10.3.3Dynamic initialization of non-block variables"))
exits via an exception, or
- [(1.6)](#except.terminate-1.6)
when destruction of an object with static or thread storage duration exits
via an exception ([[basic.start.term]](basic.start.term "6.10.3.4Termination")), or
- [(1.7)](#except.terminate-1.7)
when execution of a function registered withstd::atexit or std::at_quick_exit exits via an exception ([[support.start.term]](support.start.term "17.5Startup and termination")), or
- [(1.8)](#except.terminate-1.8)
when a[*throw-expression*](expr.throw#nt:throw-expression "7.6.18Throwing an exception[expr.throw]") ([[expr.throw]](expr.throw "7.6.18Throwing an exception"))
with no operand attempts to rethrow an exception and no exception is being
handled ([[except.throw]](except.throw "14.2Throwing an exception")), or
- [(1.9)](#except.terminate-1.9)
when the function std::nested_exception::rethrow_nested is called for an object
that has captured no exception ([[except.nested]](except.nested "17.9.8nested_­exception")), or
- [(1.10)](#except.terminate-1.10)
when execution of the initial function of a thread exits via
an exception ([[thread.thread.constr]](thread.thread.constr "32.4.3.3Constructors")), or
- [(1.11)](#except.terminate-1.11)
for a parallel algorithm whose ExecutionPolicy specifies such
behavior ([[execpol.seq]](execpol.seq "26.3.6.3Sequenced execution policy"), [[execpol.par]](execpol.par "26.3.6.4Parallel execution policy"), [[execpol.parunseq]](execpol.parunseq "26.3.6.5Parallel and unsequenced execution policy")),
when execution of an element access function ([[algorithms.parallel.defns]](algorithms.parallel.defns "26.3.1Preamble"))
of the parallel algorithm exits via an exception ([[algorithms.parallel.exceptions]](algorithms.parallel.exceptions "26.3.4Parallel algorithm exceptions")), or
- [(1.12)](#except.terminate-1.12)
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.destr "32.4.3.4Destructor"), [[thread.thread.assign]](thread.thread.assign "32.4.3.5Assignment")), or
- [(1.13)](#except.terminate-1.13)
when a call to a wait(), wait_until(), or wait_for() function on a condition variable ([[thread.condition.condvar]](thread.condition.condvar "32.7.4Class condition_­variable"), [[thread.condition.condvarany]](thread.condition.condvarany "32.7.5Class condition_­variable_­any"))
fails to meet a postcondition, or
- [(1.14)](#except.terminate-1.14)
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.mem "32.3.5.3Member functions"), [[stopsource.inplace.mem]](stopsource.inplace.mem "32.3.9.3Member functions")),
or in the constructor ofstd::stop_callback orstd::inplace_stop_callback ([[stopcallback.cons]](stopcallback.cons "32.3.6.2Constructors and destructor"), [[stopcallback.inplace.cons]](stopcallback.inplace.cons "32.3.10.2Constructors and destructor"))
when a callback invocation exits via an exception, or
- [(1.15)](#except.terminate-1.15)
when a run_loop object is destroyed
that is still in the running state ([[exec.run.loop]](exec.run.loop "33.12.1execution::run_­loop")), or
- [(1.16)](#except.terminate-1.16)
when unhandled_stopped is called on
a with_awaitable_senders<T> object ([[exec.with.awaitable.senders]](exec.with.awaitable.senders "33.13.2execution::with_­awaitable_­senders"))
whose continuation is not a handle to a coroutine
whose promise type has an unhandled_stopped member function, or
- [(1.17)](#except.terminate-1.17)
when an object scope of typestd::execution::simple_counting_scope orstd::execution::counting_scope is destroyed andscope.*state* is not equal to*joined*,*unused*, or*unused-and-closed* ([[exec.simple.counting.ctor]](exec.simple.counting.ctor "33.14.2.2.2Constructor and Destructor")), or
- [(1.18)](#except.terminate-1.18)
when std::execution::get_parallel_scheduler is called andstd::execution::system_context_replaceability::query_parallel_scheduler_backend() returns a null pointer value ([[exec.par.scheduler]](exec.par.scheduler "33.15Parallel scheduler")), or
- [(1.19)](#except.terminate-1.19)
when an exception is thrown from a coroutine std::execution::task ([[exec.task]](exec.task "33.13.6execution::task"))
which doesn't support a std::execution::set_error_t(std::exception_ptr) completion[.](#except.terminate-1.sentence-3)
— *end note*]
[2](#except.terminate-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/exceptions.tex#L1149)
In the situation where no matching handler is found, it isimplementation-defined
whether or not the stack is unwound
before std::terminate is invoked[.](#except.terminate-2.sentence-1)
In the situation where the search for a handler ([[except.handle]](except.handle "14.4Handling an exception"))
exits the function body of a function
with a non-throwing exception specification ([[except.spec]](except.spec "14.5Exception specifications")), it isimplementation-defined
whether the stack is unwound, unwound partially, or not unwound at all
before the function std::terminate is invoked[.](#except.terminate-2.sentence-2)
In all other situations, the stack shall not be unwound before
the function std::terminate is invoked[.](#except.terminate-2.sentence-3)
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[.](#except.terminate-2.sentence-4)