Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/terminate"

From cppreference.com
< cpp‎ | error
m (merge noexcept)
m (See also: +breakpoint() because of #Notes.)
 
(11 intermediate revisions by 6 users not shown)
Line 2: Line 2:
 
{{cpp/error/navbar}}
 
{{cpp/error/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | exception}}
+
{{dcl header|exception}}
{{dcl rev begin}}
+
{{dcl rev multi
{{dcl | until=c++11 |
+
|dcl1=
 
void terminate();
 
void terminate();
}}
+
|since2=c++11|dcl2=
{{dcl | since=c++11 |
+
 
[[noreturn]] void terminate() noexcept;
 
[[noreturn]] void terminate() noexcept;
 
}}
 
}}
{{dcl rev end}}
 
 
{{dcl end}}
 
{{dcl end}}
  
{{tt|std::terminate()}} is called by the C++ runtime when exception handling fails for any of the following reasons:
+
{{tt|std::terminate()}} is called by the C++ runtime when the program cannot continue for any of the following reasons:
  
@1@ an [[cpp/language/throw|exception is thrown]] and not caught (it is implementation-defined whether any stack unwinding is done in this case)
+
@1@ An [[cpp/language/throw|exception is thrown]] and not caught (it is implementation-defined whether any stack unwinding is done in this case).
  
@2@ an exception is thrown during exception handling (e.g. from a destructor of some local object, or from a function that had to be called during exception handling)
+
@2@ 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).
  
@3@ the constructor or the destructor of a static or thread-local object throws an exception
+
@3@ The constructor or the destructor of a static {{rev inl|since=c++11|or thread-local}} object throws an exception.
  
@4@ a function registered with {{lc|std::atexit}} or {{lc|std::at_quick_exit}} throws an exception
+
@4@ A function registered with {{lc|std::atexit}} {{rev inl|since=c++11|or {{lc|std::at_quick_exit}}}} throws an exception.
  
@5@ a [[cpp/language/noexcept_spec|noexcept specification]] is violated (it is implementation-defined whether any stack unwinding is done in this case)
+
{{rrev|until=c++17|
 +
@5@ A [[cpp/language/except spec|dynamic exception specification]] is violated and the default handler for {{lc|std::unexpected}} is executed.
  
{{rev begin}}
+
@6@ A non-default handler for {{lc|std::unexpected}} throws an exception that violates the previously violated dynamic exception specification, if the specification does not include {{lc|std::bad_exception}}.
{{rev|until=c++17|
+
@6@ a [[cpp/language/except_spec|dynamic exception specification]] is violated and the default handler for {{lc|std::unexpected}} is executed
+
 
+
@7@ a non-default handler for {{lc|std::unexpected}} throws an exception that violates the previously violated dynamic exception specification, if the specification does not include {{lc|std::bad_exception}}
+
 
}}
 
}}
{{rev end}}
 
  
@8@ {{c|std::nested_exception::rethrow_nested}} is called for an object that isn't holding a captured exception
+
{{rrev|since=c++11|
 +
@7@ A [[cpp/language/noexcept spec|noexcept specification]] is violated (it is implementation-defined whether any stack unwinding is done in this case).
  
@9@ an exception is thrown from the initial function of {{lc|std::thread}}
+
@8@ {{lc|std::nested_exception::rethrow_nested}} is called for an object that isn't holding a captured exception.
  
@10@ a joinable {{lc|std::thread}} is destroyed or assigned to
+
@9@ An exception is thrown from the initial function of {{lc|std::thread}}.
  
{{rev begin}}
+
@10@ A joinable {{lc|std::thread}} is destroyed or assigned to.
{{rev|since=c++17|
+
 
@11@ a function invoked by a [[cpp/algorithm|parallel algorithm]] exits via an uncaught exception and the [[cpp/algorithm/execution_policy_tag_t|execution policy]] specifies termination.
+
@11@ {{lc|std::condition_variable::wait}}, {{lc|std::condition_variable::wait_until}}, or {{lc|std::condition_variable::wait_for}} fails to reach its postcondition (e.g. if relocking the mutex throws).<!-- LWG 2135 -->
 +
}}
 +
 
 +
{{rrev|since=c++17|
 +
@12@ A function invoked by a [[cpp/algorithm|parallel algorithm]] exits via an uncaught exception and the [[cpp/algorithm/execution_policy_tag_t|execution policy]] specifies termination.
 
}}
 
}}
{{rev end}}
 
  
 
{{tt|std::terminate()}} may also be called directly from the program.
 
{{tt|std::terminate()}} may also be called directly from the program.
  
 +
When {{tt|std::terminate}} is called due to a thrown exception, an implicit try/catch handler is considered active. Thus, calling {{lc|std::current_exception}} will return the thrown exception.
  
 
In any case, {{tt|std::terminate}} calls the currently installed {{lc|std::terminate_handler}}. The default {{lc|std::terminate_handler}} calls {{lc|std::abort}}.
 
In any case, {{tt|std::terminate}} calls the currently installed {{lc|std::terminate_handler}}. The default {{lc|std::terminate_handler}} calls {{lc|std::abort}}.
  
{{rev begin}}
+
{{rrev multi
{{rev|until=c++11|
+
|rev1=
 
If a destructor reset the terminate handler during stack unwinding and the unwinding later led to {{tt|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 and the unwinding later led to {{tt|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)
}}
+
|since2=c++11|rev2=
{{rev|since=c++17|
+
 
If a destructor reset the terminate handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to {{tt|terminate}} being called.
 
If a destructor reset the terminate handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to {{tt|terminate}} being called.
 
}}
 
}}
{{rev end}}
 
  
===Parameters===
+
===Notes===
(none)
+
If the handler mechanism is not wanted, e.g. because it requires atomic operations which may bloat binary size, a direct call to {{lc|std::abort}} is preferred when terminating the program abnormally.
 +
 
 +
Some compiler intrinsics, e.g. [https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html {{tt|__builtin_trap}}] (gcc, clang, and icc) or [https://docs.microsoft.com/en-us/cpp/intrinsics/debugbreak?view=msvc-160 {{tt|__debugbreak}}] (msvc), can be used to terminate the program as fast as possible.
  
===Return value===
+
===Defect reports===
(none)
+
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2111|std=C++11|before=effect of calling {{lc|std::set_terminate}} during stack<br>unwinding differs from C++98 and breaks some ABIs|after=made unspecified}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/error/dsc terminate_handler}}
+
{{dsc inc|cpp/error/dsc terminate_handler}}
 +
{{dsc inc|cpp/utility/program/dsc abort}}
 +
{{dsc inc|cpp/utility/dsc breakpoint}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/error/terminate]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/error/terminate]]
+
[[fr:cpp/error/terminate]]
+
[[it:cpp/error/terminate]]
+
[[ja:cpp/error/terminate]]
+
[[pt:cpp/error/terminate]]
+
[[ru:cpp/error/terminate]]
+
[[zh:cpp/error/terminate]]
+

Latest revision as of 09:57, 9 July 2024

 
 
 
Defined in header <exception>
void terminate();
(until C++11)
[[noreturn]] void terminate() noexcept;
(since C++11)

std::terminate() is called by the C++ runtime when the program cannot continue for any of the following reasons:

1) An exception is thrown and not caught (it is implementation-defined whether any stack unwinding is done in this case).
2) 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).
3) The constructor or the destructor of a static or thread-local(since C++11) object throws an exception.
4) A function registered with std::atexit or std::at_quick_exit(since C++11) throws an exception.
5) A dynamic exception specification is violated and the default handler for std::unexpected is executed.
6) A non-default handler for std::unexpected throws an exception that violates the previously violated dynamic exception specification, if the specification does not include std::bad_exception.
(until C++17)
7) A noexcept specification is violated (it is implementation-defined whether any stack unwinding is done in this case).
8) std::nested_exception::rethrow_nested is called for an object that isn't holding a captured exception.
9) An exception is thrown from the initial function of std::thread.
10) A joinable std::thread is destroyed or assigned to.
11) std::condition_variable::wait, std::condition_variable::wait_until, or std::condition_variable::wait_for fails to reach its postcondition (e.g. if relocking the mutex throws).
(since C++11)
12) A function invoked by a parallel algorithm exits via an uncaught exception and the execution policy specifies termination.
(since C++17)

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)

(until 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.

(since C++11)

[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

[edit] See also

the type of the function called by std::terminate
(typedef) [edit]
causes abnormal program termination (without cleaning up)
(function) [edit]
pauses the running program when called
(function) [edit]