#include using namespace std; class UnluckyNumber : public exception { public: UnluckyNumber(const char* msg) { mMsg = msg; } virtual const char* what() const throw() { return mMsg; } private: const char* mMsg; }; class Cow { public: Cow() { cout << "Moo!" << endl; } ~Cow() { cout << "x_x" << endl; } }; int pickANumber(int n) { cout << "entering pickANumber()" << endl; Cow c; if(n % 2 == 0){ throw UnluckyNumber("That is not a good number."); } if(n % 7 == 0){ throw UnluckyNumber("That's a terrible number!"); } if(n % 13 == 0){ throw -1; } cout << "exiting pickANumber()" << endl; return n; } int main(int argc, char* argv[]) { //cout << "The first number is..." << pickANumber(5) << endl; try { cout << "The second number is..." << pickANumber(2) << endl; //cout << "Now let's try 13: " << pickANumber(13) << endl; } catch (UnluckyNumber& e) { cout << "whew, that didn't work: " << e.what() << endl; } catch (exception& e) { cout << "whew, caught an exception: " << e.what() << endl; } catch (int& e) { cout << "uh, why did you throw that??" << e << endl; } return(0); }