#include #include #include #include // for bad_alloc #include // for bad_cast #include using namespace std; class Base { public: virtual void funk() {} virtual ~Base() {} }; class Derived : public Base { public: void funk() {} }; int main() { // test bad_alloc try { while (1) { cout << "Can I have some memory?\n"; new char[0x7fffffff]; } } catch(const bad_alloc& error) { cerr << "*** I caught a " << error.what() << endl << endl; } // test bad_cast // A bad_cast exception is thrown from a dynamic_cast when a class // object or reference cannot be converted to a reference to some other class try { Base baseObject; // try to cast a base object to a derived object Derived& ref2Derived = dynamic_cast(baseObject); } catch(const bad_cast& error) { cerr << "!!! I caught a " << error.what() << endl << endl; } // test out_of_range error try { string S = "Hey"; cout << "S.at(2)=" << S.at(2) << endl; cout << "S.at(5)=" << S.at(5) << endl; // string throws an out_of_range error } catch (const out_of_range& error) { cout << "$$$ I caught a " << error.what() << endl << endl; } cout << "*** End of Program ***\n"; }