///////////////////////////////////////////////////////////////////////////// // exception.cpp - demonstrate Standard C++ exceptions // // Jim Fawcett, ELE591/791 - Intelligent Systems, Summer 2000 ///////////////////////////////////////////////////////////////////////////// #include #include using namespace std; ///////////////////////////////////////////////////////////////////////////// // function call tracer class trace { public: trace(char *s) : _s(s) { cout << "\n\n entering " << _s; } ~trace() { cout << "\n leaving " << _s; } private: char *_s; }; ///////////////////////////////////////////////////////////////////////////// // user defined exception class userException { public: userException() : _name("userException") { } const char* what() const throw() { return _name.c_str(); } private: string _name; }; ///////////////////////////////////////////////////////////////////////////// // better user defined exception - because its derived from exception base class betterUserException : public exception { public: betterUserException() : exception("betterUserException") { } virtual const char* what() const { return exception::what(); } void saveNumber(int num) { _number = num; } int savedNumber() { return _number; } private: int _number; }; // ///////////////////////////////////////////////////////////////////////////// // test divide by zero void divideByZero() { trace("divideByZero()"); try { int x = 0, y = 1; int z = y/x; } catch(const exception &e) { cout << "\n caught " << typeid(e).name(); } catch(...) { cout << "\n caught non-standard exception"; } } ///////////////////////////////////////////////////////////////////////////// // test manually throwing standard exceptions void throwStdException(exception &e) { trace("throwStdException(exception &e)"); try { throw e; } catch(const exception &e) { cout << "\n caught " << e.what(); } catch(...) { cout << "\n caught non-standard exception"; } } ///////////////////////////////////////////////////////////////////////////// // test throwing user defined exception void throwUserException(userException &me) { trace("throwUserException(userException &e)"); try { throw me; } catch(const userException &e) { cout << "\n caught " << e.what(); } catch(...) { cout << "\n caught non-standard exception"; } } // ///////////////////////////////////////////////////////////////////////////// // test throwing better user defined exception void throwBetterUserException(betterUserException &bue) { trace("throwBetterUserException(betterUserException &bue)"); cout << "\nHere in throwBetterUserException(...) I'm saving a number"; cout.flush(); bue.saveNumber(10); throw bue; } ///////////////////////////////////////////////////////////////////////////// // test stub void main() { cout << "\n Demonstrate Standard and User Defined Exceptions " << "\n =================================================="; try { divideByZero(); bad_alloc ba; throwStdException(ba); // catches too bad_cast bc; throwStdException(bc); // catches too userException ue; throwUserException(ue); // catches too betterUserException bue; // catches too bue.saveNumber(10); throwStdException(bue); throwBetterUserException(bue); // does not catch its thrown exception } catch(betterUserException &e) { cout << "\nHere in Main:"; cout << "\n caught " << e.what(); cout << "\n savedNumber = " << e.savedNumber(); } catch(const userException &e) { cout << "\n caught " << e.what(); } catch(...) { cout << "\n caught non-standard exception"; } cout << "\n\n"; }