std::set_terminate(3)
π NAME π
okay so welcome to std::set_terminate(3), bestie β this is the C++ Standard Libary manual page for std::set_terminate β std::set_terminate. It's giving main-character energy for your global terminate handler, fr fr. π
β¨ Synopsis β¨
Defined in header <exception>
std::terminate_handler set_terminate( std::terminate_handler f )
(until C++11)
throw();
std::terminate_handler set_terminate( std::terminate_handler f )
(since C++11)
noexcept;
Okay so here's the vibe: it makes f the new global terminate handler function and returns the previously installed std::terminate_handler. β¨
β οΈ bestie, f shall terminate execution of the program without returning to its caller β otherwise the behavior is undefined. No cap, do not return from it. π
This function is thread-safe, and β since C++11 β every call to std::set_terminate synchronizes-with (see std::memory_order) subsequent calls to std::set_terminate and std::get_terminate. π«Ά
π Parameters π
fβ pointer to function of typestd::terminate_handler, or null pointer.
πΈ Return value πΈ
The previously-installed terminate handler, or a null pointer value if none was installed. Periodt. π
π₯ Example π₯
// Run this code π
#include <cstdlib>
#include <exception>
#include <iostream>
int main()
{
std::set_terminate([]()
{
std::cout << "Unhandled exception\n" << std::flush;
std::abort();
});
throw 1;
}
Possible output:
Unhandled exception
bash: line 7: 7743 Aborted (core dumped) ./a.out
The terminate handler will also work for launched threads, so it can be used as an alternative to wrapping the thread function with a try/catch block. In the following example, since the exception is unhandled, std::terminate will be called. π§΅β¨
// Run this code π
#include <iostream>
#include <thread>
void run()
{
throw std::runtime_error("Thread failure");
}
int main()
{
try
{
std::thread t{run};
t.join();
return EXIT_SUCCESS;
}
catch (const std::exception& exc)
{
std::cerr << "Exception: " << exc.what() << '\n';
}
catch (...)
{
std::cerr << "Unknown exception caught\n";
}
return EXIT_FAILURE;
}
Possible output:
terminate called after throwing an instance of 'std::runtime_error'
what(): Thread failure
Aborted (core dumped)
With the introduction of the terminate handler, the exception thrown from the non-main thread can be analyzed, and exit can be gracefully performed. π
// Run this code π
#include <iostream>
#include <thread>
class foo
{
public:
foo() { std::cerr << "foo::foo()\n"; }
~foo() { std::cerr << "foo::~foo()\n"; }
};
// Static object, expecting destructor on exit
foo f;
void run()
{
throw std::runtime_error("Thread failure");
}
int main()
{
std::set_terminate([]()
{
try
{
std::exception_ptr eptr{std::current_exception()};
if (eptr)
{
std::rethrow_exception(eptr);
}
else
{
std::cerr << "Exiting without exception\n";
}
}
catch (const std::exception& exc)
{
std::cerr << "Exception: " << exc.what() << '\n';
}
catch (...)
{
std::cerr << "Unknown exception caught\n";
}
std::exit(EXIT_FAILURE);
});
std::thread t{run};
t.join();
}
Output:
foo::foo()
Exception: Thread failure
foo::~foo()
π¦ See also π¦
terminateβ function called when exception handling fails (function)get_terminate(C++11) β obtains the current terminate_handler (function)terminate_handlerβ the type of the function called bystd::terminate(typedef)
mwah π sign-off: source http://cppreference.com, version/date 2024.06.10, page std::set_terminate(3). Stay iconic, bestie! ππ
π¦ 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