1.catch clause argument is an object
Look at the code first:
#include <iostream> #include <exception>using namespace Std;class myexception:p ublic exception{public: MyException () {cout<< "myexception ()" <<ENDL;} ~myexception () {cout<< "~myexception ()" <<ENDL;} MyException (const myexception& RHS) {cout<< "myexception (const myexception& RHS)" <<ENDL;} void what () {cout<< "MyException What ()" <<endl;}}; Class Derivedexception:public myexception{public:derivedexception () {cout<< "derivedexception ()" <<endl ;} ~derivedexception () {cout<< "~derivedexception ()" <<ENDL;} A derived class that does not display a copy constructor that calls the base class implicitly calls the base class's default constructor derivedexception (const derivedexception& RHS): MyException (RHS) {cout << "derivedexception (const derivedexception& RHS)" <<ENDL;} void what () {cout<< "derivedexception What ()" <<endl;}}; void Exceptiontest () {Try{throw derivedexception ();//This is not a local object and does not destroy}catch (MyException ex) with Exceptiontest () Ex.what (); throw; The original exception object was re-thrown; }}int Main () {Try{exceptionteSt ();} catch (Derivedexception ex) {ex.what ();} return 0;}Program Run Result:
2. When the catch clause is a reference parameter
The code is as follows:
#include <iostream> #include <exception>using namespace Std;class myexception:p ublic exception{public: MyException () {cout<< "myexception ()" <<ENDL;} ~myexception () {cout<< "~myexception ()" <<ENDL;} MyException (const myexception& RHS) {cout<< "myexception (const myexception& RHS)" <<ENDL;} void what () {cout<< "MyException What ()" <<endl;}}; Class Derivedexception:public myexception{public:derivedexception () {cout<< "derivedexception ()" <<endl ;} ~derivedexception () {cout<< "~derivedexception ()" <<ENDL;} A derived class that does not display a copy constructor that calls the base class implicitly calls the base class's default constructor derivedexception (const derivedexception& RHS): MyException (RHS) {cout << "derivedexception (const derivedexception& RHS)" <<ENDL;} void what () {cout<< "derivedexception What ()" <<endl;}}; void Exceptiontest () {Try{throw derivedexception ();//This is not a local object and will not be destroyed with Exceptiontest ()}catch (MyException & Ex) {ex.what (); throw;//If a change is made to ex, the change is propagated to the next catch clause}}int main () {TRY{exceptiontest ();} catch (Derivedexception &ex) {ex.what ();} return 0;}
The results of the program run as follows:
C + + Exception object