#include using namespace std; class MyException: public std::exception { public: const char* what() const throw() { return "Just another std::exception"; } }; int main() { try { int option; cout << "Enter option (1-5): "; cin >> option; char c; MyException ex; switch(option) { case 1: throw 10; // throw an int literal break; case 2: throw 2.5; //throw a double literal break; case 3: throw "C++"; //throw a char * literal break; case 4: throw string("C++"); //throw a std::string break; case 5: throw ex; //throw a MyException object break; default: c = -10; throw c; // throw a character break; } } catch(int ex) { cout << "Got '"<< ex <<"'!\n"; } catch(double ex) { cout << "Got '"<< ex <<"'!\n"; } catch(const char *ex) { cout << "Got char* '"<< ex <<"'!\n"; } catch(const string &ex) { cout << "Got string '"<< ex <<"'!\n"; } catch(const MyException &ex) { cout << "Got '"<< ex.what() <<"'!\n"; } catch(...) { // catch any exception not caught above! cout << "Got an exception of unknown type!\n"; } cout << "Successfully handled the created exception!\n"; }