std::terminate
(C++17)<exception>
std::terminate()
is called by the C++ runtime when the program cannot continue for any of the following reasons:
std::terminate()
may also be called directly from the program.
When std::terminate
is called due to a thrown exception, an implicit try/catch handler is considered active. Thus, calling std::current_exception will return the thrown exception.
In any case, std::terminate
calls the currently installed std::terminate_handler . The default std::terminate_handler calls std::abort .
If a destructor reset the terminate handler during stack unwinding and the unwinding later led to terminate
being called, the handler that was installed at the end of the throw expression is the one that will be called. (note: it was ambiguous whether re-throwing applied the new handlers)
If a destructor reset the terminate handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to terminate
being called.
[edit] Notes
If the handler mechanism is not wanted, e.g. because it requires atomic operations which may bloat binary size, a direct call to std::abort is preferred when terminating the program abnormally.
Some compiler intrinsics, e.g. __builtin_trap
(gcc, clang, and icc) or __debugbreak
(msvc), can be used to terminate the program as fast as possible.
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2111 | C++11 | effect of calling std::set_terminate during stack unwinding differs from C++98 and breaks some ABIs |
made unspecified |