未處理的 C++ 例外狀況

如果符合的處理常式 (或省略攔截處理常式) 找不到目前例外狀況,預先定義terminate會呼叫執行階段函式。(您可以同時明確地呼叫terminate在任何您的處理常式。) 預設動作是terminate就是呼叫abort。如果您想要terminate呼叫程式中的其他函式結束應用程式之前,呼叫set_terminate做為單一引數呼叫的函式名稱的函式。您可以呼叫set_terminate您的程式中任何一點。terminate 常式永遠呼叫的最後一個函式的引數所給定set_terminate。

範例

下列範例會擲回char *例外狀況,但不包含指定用來擷取型別的例外狀況的處理常式char *。若要呼叫set_terminate會指示terminate呼叫term_func。

// exceptions_Unhandled_Exceptions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
void term_func() {
   cout << "term_func was called by terminate." << endl;
   exit( -1 );
}
int main() {
   try
   {
      set_terminate( term_func );
      throw "Out of memory!"; // No catch handler for this exception
   }
   catch( int )
   {
      cout << "Integer exception raised." << endl;
   }
   return 0;
}

Output

term_func was called by terminate.

term_func函式應該結束程式或目前的執行緒,理想的狀況是藉由呼叫exit。如果原本沒有的話,而非傳回至呼叫端, abort呼叫。

請參閱

參考

C + + 例外處理