function
<exception>

std::set_terminate

terminate_handler set_terminate (terminate_handler f) throw();
terminate_handler set_terminate (terminate_handler f) noexcept;
Set terminate handler function
Sets f as the terminate handler function.

A terminate handler function is a function automatically called when the exception handling process has to be abandoned for some reason. This happens when no catch handler can be found for a thrown exception, or for some other exceptional circumstance that makes impossible to continue the exception handling process.

Before this function is called by the program for the first time, the default behavior is to call abort.

A program may explicitly call the current terminate handler function by calling terminate,

Parameters

f
Function that takes no parameters and returns no value (void).
The function shall terminate execution of the program without returning to the caller.
terminate_handler is a function pointer type taking no parameters and returning void.

Return value

The previous terminate handler function, if any. This may be a null-pointer.
terminate_handler is a function pointer type taking no arguments and returning no value.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// set_terminate example
#include <iostream>       // std::cerr
#include <exception>      // std::set_terminate
#include <cstdlib>        // std::abort

void myterminate () {
  std::cerr << "terminate handler called\n";
  abort();  // forces abnormal termination
}

int main (void) {
  std::set_terminate (myterminate);
  throw 0;  // unhandled exception: calls terminate handler
  return 0;
}


Possible output:

terminate handler called
Aborted

Data races

Calling this function does not introduce a data race, and any such calls are synchronized with subsequent calls to set_terminate and get_terminate.

Notice that this requirement applies only to the set_terminate function, but not necessarily to the terminate handler function passed as argument (f).

Exception safety

No-throw guarantee: this function (set_terminate) never throws exceptions.

Notice that if f is a function that does not implement the proper functionality (described above), or if f is an invalid or null pointer, it causes undefined behavior.

See also