// Predictionstudy. CPP: defines the entry point for the console application. <br/> // <br/> # include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; <br/> class cmyexception <br/>{< br/> public: <br/> cmyexception (void) <br/>{< br/> cout <"cmyexception constructor" <Endl; <br/>}< br/> cmyexception (const cmyexception & OTH) <br/>{< br/> cout <"cmyexception copy" <Endl; <br/>}< br/> void dispaly (void) <br/>{< br/> cout <"display" <Endl; <br/>}< br/>}; <br/> void testfun (void); <br/> void testfun (void) <br/>{< br/> // 0 <br/> cmyexception E; <br/> throw E; <br/>}< br/>/* <br/> local objects cannot be stored locally, instead, the throw expression is used to initialize a special object called an exception object. <br/> the exception object is managed by the compiler, additionally, make sure that the resident can access any catch that may be activated <br/> */</P> <p> class cmyexceptionbase <br/>{< br/> public: <br/> cmyexceptionbase (void) <br/>{< br/> cout <"cmyexceptionbase constructor" <Endl; <br/>}< br/> cmyexceptionbase (const cmyexceptionbase & OTH) <br/>{< br/> cout <"cmyexceptionbase copy" <Endl; <br/>}< br/> virtual void display (void) <br/>{< br/> cout <"cmyexceptionbase display" <Endl; <br/>}< br/>}; <br/> class cmyexceptiondrive: Public cmyexceptionbase <br/>{< br/> Public: <br/> cmyexceptiondrive (void) <br/>{ <br/> cout <"cmyexceptiondrive constructor" <Endl; <br/>}< br/> cmyexceptiondrive (const cmyexceptiondrive & OTH) <br/>{< br/> cout <"cmyexceptiondrive copy" <Endl; <br/>}< br/> void display (void) <br/>{< br/> cout <"cmyexceptiondrive display" <Endl; <br/>}< br/> }; <br/> void testfun2 (void); <br/> void testfun2 (void) <br/>{< br/> cmyexceptiondrive E; <br/> cmyexceptionbase * P = & E; <br/> throw * P; // object cutting, the execution is cmyexceptionbase copying the constructor construction exception object <br/>}< br/> // about object cutting <br/> class cobjbase <br/>{< br/> Public: <br/> cobjbase (void) <br/>{< br/> cout <"cobjbase constructor" <Endl; <br/>}< br/> cobjbase (const cobjbase & OTH) <br/>{< br/> cout <"cobjbase copy" <Endl; <br/>}< br/> virtual void fun1 (void) <br/>{< br/> cout <"cobjbase fun1" <Endl; <br/>}< br/>}; <br/> class cobjdrive: Public cobjbase <br/>{< br/> Public: <br/> cobjdrive (void) <br/>{< br/> cout <"cobjdrive constructor" <Endl; <br/>}< br/> cobjdrive (const cobjdrive & OTH) <br/>{< br/> cout <"cobjdrive copy" <Endl; <br/>}< br/> void fun1 (void) <br/>{< br/> cout <"cobjdrive fun1" <Endl; <br/>}< br/> }; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/> {<br/> // 00 <br/> try <br/> {<br/> testfun (); <br/>}< br/> catch (cmyexception e) <br/>{< br/> E. dispaly (); <br/>}< br/>/* <br/> cmyexception constructor <br/> copy of the expression thrown by cmyexception copy <br/> copy of cmyexception copy value <br/> display <br/> press any key to continue... <br/> */<br/> // 01 <br/> try <br/> {<br/> testfun (); <br/>}< br/> catch (cmyexception & E) // directly reference the exception object <br/>{< br/> E. dispaly (); <br/>}< br/>/* <br/> cmyexception constructor <br/> cmyexception copy <br/> display <br/> press any key to continue... <br/> */<br/> cout <"222222222222222222222222222222222222222222222222222222222222222222" <Endl; <br/> try <br/>{< br/> testfun2 (); <br/>}< br/> catch (cmyexceptionbase e) <br/>{< br/> E. display (); <br/>}< br/>/* <br/> cmyexceptionbase constructor <br/> cmyexceptiondrive constructor <br/> copy of the expression thrown by cmyexceptionbase copy copy constructor) generate exception object <br/> cmyexceptionbase copy is not referenced, and the value is copied <br/> cmyexceptionbase display <br/> press any key to continue... <br/> */<br/> cout <"33333333333333333333333333333333333333333333333333333333333333333333" <Endl; <br/> cobjdrive OBJ; <br/> cobjbase * P = & OBJ; <br/> P-> fun1 (); <br/> obj. fun1 (); <br/> (cobjbase) OBJ ). fun1 (); // send object cutting, call the base class copy constructor to construct a temporary object <br/>/* <br/> the exception specifier of the base class can be used to capture exceptions of the derived type <br/> * /<br/> cout <"444444444444444444444444444444444444444444444444444444444" <Endl; <br/> try <br/> {<br/> cmyexceptiondrive Haha; <br/> throw Haha; // call cmyexceptiondrive to copy the constructor to generate an exception object, object cutting calls cmyexceptionbase to copy the constructor to generate a cmyexceptionbase object <br/>}< br/> catch (cmyexceptionbase e) <br/>{< br/> E. display (); <br/>}< br/>/* <br/> cmyexceptionbase constructor <br/> cmyexceptiondrive constructor <br/> cmyexceptionbase constructor <br/> cmyexceptiondrive copy <br/> cmyexceptionbase copy <br/> cmyexceptionbase display <br/> press any key to continue... <br/> */<br/> return 0; <br/>}< br/>/* <br/> note <br/> destructor should never throw an exception (when expanding for an exception stack, if an unhandled exception is thrown by the destructor, the standard library terminate function will be called) <br/> if an exception is thrown in the constructor, you must properly revoke the constructed members. <br/> */