// ========================================================================== // $Id: exception_intro.cpp,v 1.2 2014/10/22 20:26:07 jlang Exp $ // CSI2372 example Code for lecture 7 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: exception_intro.cpp,v $ // Revision 1.2 2014/10/22 20:26:07 jlang // Update for use of array and forward_list. // // Revision 1.1 2006/10/16 01:44:37 jlang // small caps names // // Revision 1.1 2006/10/16 01:36:18 jlang // Check in for lecture 7 // // // ========================================================================== #include #include using std::cin; using std::cout; using std::cerr; using std::endl; using std::string; int main () { int myInt; cout << "Number? "; try { // try block cin >> myInt; if ( cin.fail() ) { string line; getline(cin,line); throw line; } if ( myInt < 0 ) throw myInt; } catch ( int i ) { // Integer exception handler cerr << "Error: Input: " << i << endl; } catch (...) { // Catch anything else cerr << "Error: Unexpected input failure" << endl; throw; // re-throw exception } return 0; }