Difference between std::terminate
and std::abort
What is the difference between std::terminate()
and std::abort()
in C++?
Both std::terminate()
and std::abort()
are used to abnormally terminate a C++ program, but they have some differences:
std::terminate()
:
- Invoked when an exception is not caught and propagates out of
main()
, or when an exception is thrown from a destructor during stack unwinding. - Can be customized by setting a terminate handler using
std::set_terminate()
. - The default terminate handler calls
std::abort()
.
std::abort()
:
- Immediately terminates the program without invoking any cleanup or destructors.
- Sends an abnormal termination signal (SIGABRT) to the host environment.
- Cannot be customized.
In summary, std::terminate()
provides a customization point before terminating the program, while std::abort()
immediately terminates without any opportunity for cleanup or customization.
Using std::terminate()
and the noexcept
Specifier
This lesson explores the std::terminate()
function and noexcept
specifier, with particular focus on their interactions with move semantics.