/****************************************************************************** *** FILE: cathcing.cpp *** DATE: 03/18/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This program illustrates how to throw and catch different types of *** exceptions standard and user defined. It also shows how to set *** a user defined terminate function - the function that is to be *** executed if an exception thrown and no appropriate catch() is *** found. *** To compile with MS Visual Studio command line compiler use: *** c:\...> cl catching.cpp /EHsc /GR ******************************************************************************/ #include #include using namespace std; class UserDefinedException : public logic_error { public: UserDefinedException(const string &msg) : logic_error(msg) {}; }; void my_terminate_function( ) { cout<<"Uncaught exception detected. I'll quit.\n"<>i; switch( i ){ case 1: throw UserDefinedException("as you asked this is my own exception"); case 2: throw logic_error("some logic error occured"); case 3: throw bad_alloc("cannot allocate enough memory"); case 4: throw overflow_error("trying to divide by zero"); case 0: throw 7; // thrown exception that won't be caugth default: throw domain_error("you entered an invalid number"); } } void main() { bool time_to_stop = false; terminate_handler oldHand = set_terminate(my_terminate_function); while( ! time_to_stop ){ try{ time_to_stop = user_prompt(); } catch(UserDefinedException &e){ cerr<<"UserDefinedException caught: "<