// File: SimpleIO.cpp // Example of catching exception // Array access out of bounds... #include #include using namespace std; void readInt(int &n) throw (string) { cin >> n; if (cin.fail()) throw(string)"user input locked up cin stream"; } int main() { int n; cout << "Enter an int...Enter something else to test exception handling >"; cout.flush(); bool ok=false; while (!ok) { try { readInt(n); ok=true; } catch (ios_base::failure) { cin.clear(); cerr << "Bad input detected: " << endl; } catch (int exNum) { cin.clear(); cerr << "Bad input detected: " << exNum << endl; } catch (string s) { cerr << s << endl; } catch (...) { cerr << "General exception...\n"; } cin.clear(); } cout << "You entered " << n << endl; }