C + + Exception object

Source: Internet
Author: User

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 ()" &LT;&LT;ENDL;} ~myexception () {cout<< "~myexception ()" &LT;&LT;ENDL;} MyException (const myexception& RHS) {cout<< "myexception (const myexception& RHS)" &LT;&LT;ENDL;} void what () {cout<< "MyException What ()" <<endl;}}; Class Derivedexception:public myexception{public:derivedexception () {cout<< "derivedexception ()" <<endl ;} ~derivedexception () {cout<< "~derivedexception ()" &LT;&LT;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)" &LT;&LT;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 ()" &LT;&LT;ENDL;} ~myexception () {cout<< "~myexception ()" &LT;&LT;ENDL;} MyException (const myexception& RHS) {cout<< "myexception (const myexception& RHS)" &LT;&LT;ENDL;} void what () {cout<< "MyException What ()" <<endl;}}; Class Derivedexception:public myexception{public:derivedexception () {cout<< "derivedexception ()" <<endl ;} ~derivedexception () {cout<< "~derivedexception ()" &LT;&LT;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)" &LT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.