次の例は、制御の流れと、例外処理で使用する特殊な関数を示しています。
#include <iostream> #include <exception> using namespace std; class X { }; class Y { }; class A { }; // pfv type is pointer to function returning void typedef void (*pfv)(); void my_terminate() { cout << "Call to my terminate" << endl; abort(); } void my_unexpected() { cout << "Call to my_unexpected()" << endl; throw; } void f() throw(X,Y, bad_exception) { throw A(); } void g() throw(X,Y) { throw A(); } int main() { pfv old_term = set_terminate(my_terminate); pfv old_unex = set_unexpected(my_unexpected); try { cout << "In first try block" << endl; f(); } catch(X) { cout << "Caught X" << endl; } catch(Y) { cout << "Caught Y" << endl; } catch (bad_exception& e1) { cout << "Caught bad_exception" << endl; } catch (...) { cout << "Caught some exception" << endl; } cout << endl; try { cout << "In second try block" << endl; g(); } catch(X) { cout << "Caught X" << endl; } catch(Y) { cout << "Caught Y" << endl; } catch (bad_exception& e2) { cout << "Caught bad_exception" << endl; } catch (...) { cout << "Caught some exception" << endl; } }
次に、上記の例の出力を示します。
In first try block Call to my_unexpected() Caught bad_exception In second try block Call to my_unexpected() Call to my terminate
実行時に、このプログラムは次のように振る舞います。
例外が、my_unexpected() によって、有効な例外としてではなく、予期しないスロー (throw) として 処理されたので、2 番目の try ブロックに続く catch ブロックには、入らないことに留意してください。
関連参照