#include #include #include #include using namespace ::std; #define CASE 3 //CASE 1 domain error is upcast to logic_error, and catch (logic_error& e) is executed //CASE 2 range_error is upcast to exception, and catch exception is executed //CASE 3 unexpected points at handle, and handle is called. logic_error is thrown, and caught by catch(logic_error& e) void f() throw(logic_error, runtime_error) { #if CASE == 1 throw domain_error( "domain error label A"); #elif CASE == 2 throw range_error( "range error label B"); #elif CASE == 3 throw (5); #endif } void handle() { cout << "handle called" << endl; throw logic_error( "logic_error lable C"); } int main() { void (*prev_handle)(); prev_handle = set_unexpected( handle); cout << "prev value of unexpected = " << (void*)prev_handle << endl; cout << "abort = " << (void*)abort << endl; cout << "exit = " << (void*)exit << endl; cout << "terminate = " << (void*)terminate << endl; try { f(); } catch (domain_error& e) { cout << "domain_error at line " << __LINE__ << " e.what = " << e.what() << endl; } catch (logic_error& e) { cout << "logic_error at line " << __LINE__ << " e.what = " << e.what() << endl; } catch (exception& e) { cout << "std::exception at line " << __LINE__ << " e.what = " << e.what() << endl; } return 0; }