// except.cpp #include using namespace std; class base { public: virtual void say() { cout << "\n base object speaking"; } }; class derived : public base { public: virtual void say() { cout << "\n derived object speaking"; } }; void fun1( ) { cout << "\n throwing a base exception"; throw base(); } void fun2( ) { cout << "\n throwing a derived exception"; throw derived(); } void fun3( ) { cout << "\n throwing a pointer to a derived object"; derived d; base *pb = &d; throw pb; } void main( ) { try { fun1(); } catch(base &b) { cout << "\n in catch(base &b)"; b.say(); } try { fun2(); } catch(base &b) { cout << "\n in catch(base &b)"; b.say(); } try { fun3(); } catch(base *pb) { cout << "\n in catch(base *pb)"; pb->say(); } cout << "\n\n"; }