Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/set terminate"

From cppreference.com
< cpp‎ | error
m (s/the subsequent/subsequent calls to/)
(terminate_handler must terminate)
Line 11: Line 11:
 
{{dcl end}}
 
{{dcl end}}
  
Makes {{tt|f}} the new global terminate handler function and returns the previously installed {{lc|std::terminate_handler}}.
+
Makes {{tt|f}} the new global terminate handler function and returns the previously installed {{lc|std::terminate_handler}}. {{tt|f}} shall terminate execution of the program without returning to its caller, otherwise the behavior is undefined.
  
 
{{rrev|since=c++11|
 
{{rrev|since=c++11|

Revision as of 02:42, 27 February 2023

 
 
 
Defined in header <exception>
(until C++11)
std::terminate_handler set_terminate( std::terminate_handler f ) noexcept;
(since C++11)

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)

Contents

Parameters

f - pointer to function of type std::terminate_handler, or null pointer

Return value

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

Example

#include <iostream>
#include <cstdlib>
#include <exception>
 
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

See also

function called when exception handling fails
(function) [edit]
obtains the current terminate_handler
(function) [edit]
the type of the function called by std::terminate
(typedef) [edit]