Share via


ハンドルされていない C++ の例外

一致するハンドラー (または省略記号 Catch ハンドラー現在の例外の場合) 見つからない場合 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;
}

出力

term_func was called by terminate.

term_func の関数は exit を呼び出してプログラムまたは現在のスレッドを適切に終了する必要があります。これは呼び出し元が代わりに abort 返さない場合はが呼び出されます。

参照

関連項目

C++ 例外処理