// ========================================================================== // $Id: std_exception.cpp,v 1.2 2007/09/18 00:24:50 jlang Exp $ // CSI2372 example Code for lecture 13 // ========================================================================== // (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: std_exception.cpp,v $ // Revision 1.2 2007/09/18 00:24:50 jlang // Added comments // // Revision 1.1 2006/11/20 04:52:12 jlang // Check-in for lecture 13 // // // ========================================================================== #include #include #include #include #include using std::cout; using std::cerr; using std::endl; char getRandomChar() { int randomNumber = rand()-RAND_MAX/2; char res = static_cast(randomNumber); cout << randomNumber << " " << res << endl; if ( static_cast(res) < randomNumber ) throw std::overflow_error("Random number to large"); if ( static_cast(res) > randomNumber ) throw std::underflow_error("Random number to small"); return res; } int main() { // Seed random number generator srand( (unsigned) time(0) ); char word[16]; int pos = 0; while ( pos < 16 ) { try { word[pos] = getRandomChar(); ++pos; } catch( std::underflow_error _uError ) { cerr << _uError.what() << endl; } catch( std::overflow_error _oError ) { cerr << _oError.what() << endl; } } cout << word << endl; }