C++使用引用捕获异常

catch子句捕获异常时既可以按值传递,也可以按照引用传递,甚至按照指针传递,但推荐使用引用捕获异常。考察如下程序:

#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        cout << "Base's constructor" << endl;
    }

    Base(const Base& rb)
    {
        cout << "Base's copy constructor" << endl;
    }

    virtual void print()
    {
        cout << "Base" << endl;
    }
};

class Derived :public Base
{
public:
    Derived()
    {
        cout << "Derived's constructor" << endl;
    }

    Derived(const Derived& rd):Base(rd)
    {
        cout << "Derived's copy constructor" << endl;
    }

    virtual void print()
    {
        cout << "Derived" << endl;
    }
};

void throwFunc()
{
    Derived d;
    throw d;
}

int main()
{
    try
    {
        throwFunc();
    }
    catch (Base b)
    {
        cout << "Base catched" << endl;
        b.print();
    }
    catch (Derived d)
    {
        cout << "Derived catched" << endl;
        d.print();
    }
    cout << "---------------" << endl;
    try
    {
        throwFunc();
    }
    catch (Base& b)
    {
        cout << "Base catched" << endl;
        b.print();
    }
    catch (Derived& d)
    {
        cout << "Derived catched" << endl;
        d.print();
    }
}

程序运行结果:

Base's constructor
Derived's constructor
Base's copy constructor
Derived's copy constructor
Base's copy constructor
Base catched
Base
---------------
Base's constructor
Derived's constructor
Base's copy constructor
Derived's copy constructor
Base catched
Derived

阅读以上程序,注意以下几点:
(1)程序中在函数throwFunc()中构造对象d,先后分别调用基类Base和派生类Derived的构造函数完成对象d的初始化,分别输出Base’s constructor与Derived’s constructor;
(2)C++标准要求被作为异常抛出的对象必须被拷贝复制,导致异常对象d在离开作用域时,触发一次临时对象的拷贝构造,程序输出从结果来看,先后调用了基类Base的拷贝构造函数和派生类Derived的拷贝构造函数,分别输出Base’s copy constructor与Derived’s copy constructor;
(3)按引用捕获异常比按值捕获异常更加高效。分隔线以上按值捕获异常,导致对象d在传递时再次被拷贝一次,输出Base’s copy constructor,降低了系统效率,使用引用捕获异常可以避免额外的拷贝操作;
(4)使用引用捕获异常,可以通过基类对象实现虚函数的虚调用,在运行时提现多态性。

基于效率和多态性的考虑,建议使用引用来捕获异常。


参考文献

[1]陈刚.C++高级进阶教程[M].武汉:武汉大学出版社,2008.10.5用传引用的方式捕获异常

猜你喜欢

转载自blog.csdn.net/K346K346/article/details/81461007
今日推荐