#include #include #include class invalid_expr : public std::exception { public: virtual const char* what() const noexcept { return "this expression is bad!"; } private: int i; }; void abandon_ship() { std::cout << "Mayday mayday mayday!" << std::endl; std::exit(0); } int eval(int expr) throw (int) { // normal, VALID case if (expr > 7) return expr - 3; // absnormal, INVALID case if (expr < 0) { invalid_expr e; throw e; } switch (expr) { case 0: throw(0); // small error case 1: throw(1); // big error; case 2: throw("huge error"); default: throw("nasty error!"); } } void read_integer(int & i) { std::cout << "Input: "; std::cin >> i; } int main() { std::set_unexpected(abandon_ship); try { int i; read_integer(i); std::cout << "OUTPUT: "; std::cout << eval(i) << std::endl; } catch (int ex) { std::cout << "error number " << ex << std::endl; } catch (const char * error_msg) { std::cout << error_msg << std::endl; } catch (const std::exception & e) { std::cout << "Check out this exception: " << e.what() << std::endl; } }