Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::terminate — terminating without returning

std::terminate, declared in <exception>, invokes a program-termination handler and never returns. It is triggered by conditions such as an unhandled exception or an exception escaping a noexcept function, and can also be called explicitly. It does not guarantee destructor-based resource cleanup.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <exception>
[[noreturn]] void std::terminate() noexcept;

std::terminate() from <exception> takes no arguments and never returns. It invokes a handler of type std::terminate_handler; the default handler calls std::abort(). This is not throwing an exception or a recovery mechanism that catch can intercept.

Typical triggers include no matching catch, an exception escaping a noexcept function, a destructor exiting via an exception during stack unwinding, and destroying a std::thread object that is still joinable(). A program can also call std::terminate() explicitly.

Do not assume complete RAII cleanup. With no matching exception handler, stack unwinding is implementation-defined; when an exception escapes a noexcept function, unwinding may be complete, partial, or absent. An explicit call to std::terminate() itself does not unwind the stack.

std::set_terminate installs a custom handler. That handler must terminate the program without returning to its caller. The example installs stop, which calls std::_Exit(EXIT_FAILURE): no text output, an unsuccessful termination status, and no execution of destructors or functions registered with std::atexit. Run it as a separate process.

This entry targets C++23. The supplied pages are an evolving standard draft and also contain later additions; this entry does not attribute those additions to C++23. A repository compiler flag does not attest the production runtime.

Examples

#include <cstdlib>
#include <exception>

[[noreturn]] void stop() noexcept {
    std::_Exit(EXIT_FAILURE);
}

int main() {
    std::set_terminate(stop);
    std::terminate();
}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources