函式
<exception>

std::terminate

void terminate();
[[noreturn]] void terminate() noexcept;
處理因異常而終止的函式
呼叫當前的terminate handler

預設情況下,terminate handler 呼叫 abort。但是可以透過呼叫 set_terminate 來重新定義此行為。

當找不到已丟擲異常的 catch 處理器,或出現其他無法繼續異常處理過程的異常情況時,會自動呼叫此函式。

提供此函式是為了讓程式在需要異常終止時能夠顯式呼叫terminate handler。即使沒有呼叫 set_terminate 來設定自定義terminate handler(此時呼叫 abort),此函式也能正常工作。

返回值

無(該函式永不返回)。

示例

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;
}

可能的輸出

Attempting to allocate 1 GiB... Ok


異常安全

無異常保證:此函式從不丟擲異常。

另見