catcher_cpp
#include <cstdlib> #include <ctime> #include <iostream> #include <string> #include <stdexcept> using namespace std; int main(int argc, char **argv) { srand(time(nullptr)); for(string s:{ "alice", "faye", "billy", "sarah", "becca", "philip", "angie", "ellis", "beatrice" }) { double foo = 1.0, bar = 0.0; try { cout << s << " as hex: " << stoi(s,0,16) << std::endl; cout << "Some random characters: "; for(int i = 1; i <= 10; i++) cout << s.at(rand() % (s.length() + 2)) << " "; cout << endl; if(rand() % 10 < 9) throw overflow_error("Too much"); } catch(invalid_argument &e) { cout << "Conversion error: " << e.what() << std::endl; } catch(out_of_range &e) { cout << "\nOut of range: " << e.what() << endl; } catch(exception &e) { cout << "Exception: " << e.what() << std::endl; } cout << endl; } }