A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../error/error_code/../set_terminate.html below:

std::set_terminate - cppreference.com

Makes f the new global terminate handler function and returns the previously installed std::terminate_handler. f shall terminate execution of the program without returning to its caller, otherwise the behavior is undefined.

This function is thread-safe. Every call to std::set_terminate synchronizes-with (see std::memory_order) subsequent calls to std::set_terminate and std::get_terminate.

(since C++11) [edit] Parameters [edit] Return value

The previously-installed terminate handler, or a null pointer value if none was installed.

[edit] Example
#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.

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.

Output:

foo::foo()
Exception: Thread failure
foo::~foo()
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4