Exception handling mechanism in C + +

Source: Internet
Author: User

Catch exception mechanism in C + + the type of arguments in the catch parameter is different, the processing is not the same, and the normal function call is not the same, the specific behavior is that when throwing an exception throw A () or throw obj, the object will be an additional object copy operation.

The test class is implemented as follows:

#include <iostream>/** * Test exception thrown with virtual function */using namespace Std;class a {public:a () {cout << "a ()" << Endl;} ~a () {cout << "~a ()" << Endl;} A (const a& a) {cout << "a (a)" << Endl;} virtual void func () {cout << "A::func ()" << Endl;}; Class B:public A {B () {cout << "B ()" << Endl;} B (const b &b) {cout << "B (b)" << Endl;} ~b () {cout << "~b ()" << Endl;} virtual void func () {cout << "B::func ()" << Endl;};

1) When an exception is caught by object passing, two copy operations occur in the object, one at a time to the temporary object, and two times for temporary objects by reference to argument B.

void F1 (const A &a) {try {cout << "------function F1:--------" << endl;throw A;} catch (A b) {cout <&L T "Exception A B" << Endl;} cout << "----function F1-------" << Endl;}

The result of this code execution is as follows:

------Function F1:--------

A (a)

A (a)

Exception A B

~a ()

~a ()

----Function F1-------

2) When an exception is caught by reference, the second replication overhead is less, which is to generate the temporary object directly as a parameter.

void F2 (const a &a) {try {cout << "------function F2:--------" << endl;throw A;} catch (const a &b) { cout << "Exception A &b" << Endl;} cout << "----function f2-------" << Endl;}

The results of the implementation are as follows:

------function F2:--------A (a) exception a &b~a ()----function F2-------

3) If you throw a reference object directly, the system will error, because the exception handling mechanism is not limited to the current function, it is possible outside the function.

void F3 (const A &a) {try {cout <<------function F3:--------"<< endl;throw &a;//Reference exception, the system terminates unexpectedly} catch (const A &B) {cout << "exception A &b" << Endl;} cout << "----function f3-------" << Endl;}

4) When an exception is thrown, the object can be thrown directly, or an object can be generated, unlike the constructor non-copy constructor is explicitly called.

void F4 (const a &a) {try {cout << "------function F4:--------" << endl;throw A ();} catch (const a &b) {cout << "exception A &b" << Endl;} cout << "----function f4-------" << Endl;}

The results of the implementation are as follows:

------function F4:--------A () exception a &b~a ()----function F4-------

5) When dealing with an exception class with inheritance, exception handling rules are not handled according to the "best fit" principle in the virtual function inheritance, but rather the "first fit" principle.

void F5 (const b& b) {try {cout << "------function f5:--------" << endl;throw B;} catch (const A &a) {C Out << "Exception A &a" << Endl;} catch (const B &B1) {cout << "Exception b &b1" << Endl;} cout << "----function f5-------" << Endl;}

The exception matches a rather than B:

------function f5:--------A () b (b) exception a &a~b () ~a ()----function f5-------

Exception handling mechanism in C + +

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.