function
<exception>

std::terminate

void terminate();
[[noreturn]] void terminate() noexcept;
Function handling termination on exception
Calls the current terminate handler.

By default, the terminate handler calls abort. But this behavior can be redefined by calling set_terminate.

This function is automatically called 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.

This function is provided so that the terminate handler can be explicitly called by a program that needs to abnormally terminate, and works even if set_terminate has not been used to set a custom terminate handler (calling abort in this case).

Return value

none (the function never returns).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// terminate example
#include <iostream>       // std::cout, std::cerr
#include <exception>      // std::exception, std::terminate

int main (void) {
  char* p;
  std::cout << "Attempting to allocate 1 GiB...";
  try {
    p = new char [1024*1024*1024];
  }
  catch (std::exception& e) {
    std::cerr << "ERROR: could not allocate storage\n";
    std::terminate();
  }
  std::cout << "Ok\n";
  delete[] p;
  return 0;
}


Possible output:

Attempting to allocate 1 GiB... Ok

Exception safety

No-throw guarantee: this function never throws exceptions.

See also