終了関数を呼び出そうとしています。[この関数は、スタックの巻き戻し中に別の例外が発生したときに呼び出されると思います]。私が書いたものと同じシナリオを検証しようとしています。
terminal()関数が呼び出されているのを確認できますが、デバッグエラーが発生する理由がわかりません。
Visual Studio 2008で次のコードを実行しようとすると、エラーメッセージダイアログボックス「デバッグエラー」が表示されます。出力も表示されています:
出力:
トライブロックで
Aのコンストラクターで
Bのコンストラクターで
Bのデストラクタ
Aのデストラクタ
my_terminateへの呼び出し
このコードの実行中にこの「デバッグエラー」ウィンドウが表示されるのはなぜですか?期待される動作ですか?このエラーを取り除く方法は?
class E
{
public:
const char* message;
E(const char* arg) : message(arg) { }
};
void my_terminate()
{
cout << "Call to my_terminate" << endl;
};
class A
{
public:
A() { cout << "In constructor of A" << endl; }
~A()
{
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};
class B
{
public:
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};
void main()
{
set_terminate(my_terminate);
try
{
cout << "In try block" << endl;
A a;
B b;
throw("Exception thrown in try block of main()");
}
catch (const char* e)
{
cout << "Exception: " << e << endl;
}
catch (...)
{
cout << "Some exception caught in main()" << endl;
}
cout << "Resume execution of main()" << endl;
getch();
}