未処理の 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 が呼び出されます。