std::set_terminate

From cppreference.com
< cpp‎ | error
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
Error handling
Exception handling
(C++11)
(C++17)
Exception handling failures
(C++11)
set_terminate
(deprecated)
(deprecated)
(C++11)(deprecated)
(deprecated)
 
Defined in header <exception>

Makes f the new global terminate handler function and returns the previously installed std::terminate_handler.

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

(since C++11)

Contents

[edit] Parameters

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

[edit] Return value

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

[edit] Exceptions

(none) (until C++11)
noexcept specification:  
noexcept
  
(since C++11)

[edit] Example

#include <iostream>
#include <cstdlib>
#include <exception>
 
int main()
{
    std::set_terminate([](){ std::cout << "Unhandled exception\n"; std::abort();});
    throw 1;
}

Possible output:

Unhandled exception
bash: line 7:  7743 Aborted                 (core dumped) ./a.out

[edit] See also

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