std::terminate(3)
π NAME π
std::terminate β bestie, this is the function the C++ runtime hits you with when the program literally cannot keep going. No cap. It's the "we're done here" button, and it's from the C++ Standard Library (that's the manual we're in, mwah π
).
β¨ SYNOPSIS β¨
Ok so first things first, it lives in this header:
Defined in header <exception>
void terminate(); (until C++11)
[[noreturn]] void terminate() noexcept; (since C++11)
Note the glow-up in C++11 β it picked up [[noreturn]] and noexcept and honestly? Iconic. π
πΈ DESCRIPTION πΈ
So std::terminate() gets called by the C++ runtime when the program cannot continue, for any of these reasons (and there are twelve, so buckle up buttercup):
- An exception is thrown and not caught (it is implementation-defined whether any stack unwinding is done in this case).
- A function directly invoked by the exception handling mechanism while handling an exception that has not yet been caught exits via an exception (e.g. a destructor of some local object, or a copy constructor constructing a catch-clause parameter).
- The constructor or the destructor of a
staticorthread-local(since C++11) object throws an exception. - A function registered with
std::atexitorstd::at_quick_exit(since C++11) throws an exception. - A dynamic exception specification is violated and the default handler for
std::unexpectedis executed. - A non-default handler for
std::unexpectedthrows an exception that violates the previously violated dynamic exception specification, if the specification does not includestd::bad_exception. β this one's until C++17, so it's giving legacy β¨ - A
noexceptspecification is violated (it is implementation-defined whether any stack unwinding is done in this case). std::nested_exception::rethrow_nestedis called for an object that isn't holding a captured exception.- An exception is thrown from the initial function of
std::thread. β since C++11 - A joinable
std::threadis destroyed or assigned to. std::condition_variable::wait,std::condition_variable::wait_until, orstd::condition_variable::wait_forfails to reach its postcondition (e.g. if relocking the mutex throws).- A function invoked by a parallel algorithm exits via an uncaught exception and the execution policy specifies termination. β since C++17
Ok and heads up, std::terminate() may also be called directly from the program. Like, you can just... call it. Your choice bestie π«Ά
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. (Cute lil detail, right?)
In any case, std::terminate calls the currently installed std::terminate_handler. The default std::terminate_handler calls std::abort. That's the whole chain, no detours. π
Now the messy part, the until-C++11 vs since-C++11 drama about handlers:
Until C++11: 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)
Since C++11: 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.
So yeah β unspecified, don't build your whole personality on it. π
π¦ 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. Zoom zoom. π₯
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Yes, they went back and retconned it, main character energy fr fr.)
LWG 2111β applied to C++11. Behavior as published: effect of callingstd::set_terminateduring stack unwinding differs from C++98 and breaks some ABIs. Correct behavior: made unspecified.
β SEE ALSO β
terminate_handlerβ the type of the function called bystd::terminate(typedef)abortβ causes abnormal program termination (without cleaning up) (function)
That's the whole page, bestie! π§ Source and version: http://cppreference.com, 2024.06.10 β and yes, we were on std::terminate(3) the whole time. Go forth and don't accidentally call it. πππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms