Exception Demo
#include <stdlib.h> #include <iostream> #include <string> #include <stdexcept> using namespace std; int main(int argc, char **argv) { while(true) { try{ string s1, s2; cout << "> "; cin >> s1 >> s2; if(s1 == "stop") break; if(!cin) throw string("Input failed."); int n = stoi(s1); string part = s2.substr(n); cout << s1 << ", " << s2 << ": " << part << endl; if(s2 == "mango") throw 17; } catch(logic_error &e) { cout << "Exception: " << e.what() << endl; } catch(string &s) { cout << "String error: " << s << endl; break; } catch(...) { cout << "Unknown exception." << endl; } } }