51 异常(二)

#include <iostream>
#include <string>
using namespace std;

class MyException
{
public:
    MyException(const char* message)
        :message_(message)
    {
        cout << "MyException ..." << endl;
    }
    MyException(const MyException& other) : message_(other.message_)
    {
        cout << "Copy MyException ..." << endl;
    }
    ~MyException()
    {
        cout << "~MyException"<< endl;
    }
    const char* what() const
    {
        return message_.c_str();
    }
private:
    string message_;
};
double Divide(double a, double b)
{
    if (b == 0.0)
    {
        //MyException e("division by zero");//异常发生之前创建的局部对象被销毁,这一过程称为栈展开
        //throw e;//throw抛出一个类对象会调用拷贝构造函数
        throw MyException("division by zero");//不再调用拷贝构造函数,只调用构造函数
    }
    else
        return a / b;
}
int main(void)
{
    try
    {
        cout << Divide(5.0, 0.0) << endl;
    }
    catch (MyException& e)
    {
        cout << e.what() << endl;
    }
    /*catch (...)//...可捕获任何异常
    {
        cout <<"catch a exception ..."<< endl;
    }*/
}

这里写图片描述

拷贝构造函数的调用

#include <iostream>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "Test ..." << endl;
    }
    Test(const Test& other)
    {
        cout << "Copy Test ..." << endl;
    }
    ~Test()
    {
        cout << "~Test" << endl;
    }

};
Test fun()
{
    //Test t;//调用构造函数
    //return t;//调用拷贝构造函数
    return Test();//不会调用拷贝构造函数
}
int main(void)
{
    fun();
    return 0;
}
#include<iostream>

using namespace std;

class MyException
{
public:
    MyException(const char* message)
        :message_(message)
    {
        cout << "MyException ..." << endl;
    }
    MyException(const MyException& other) : message_(other.message_)
    {
        cout << "Copy MyException ..." << endl;
    }
    ~MyException()
    {
        cout << "~MyException" << endl;
    }
    const char* what() const
    {
        return message_.c_str();
    }
private:
    string message_;
};

int main(void)
{

    try
    {
        try
        {
            throw MyException("test exception");//抛出异常
        }
        catch (int)
        {
            cout << "catch a int exception" << endl;
        }
        catch (MyException& e)//如果内层捕获,不再传播到外面
        {
            cout << "Inner ..." << endl;
            cout << e.what() << endl;
            throw e;//重新抛出异常,可传到外部
        }
    }
    catch (MyException& e)
    {
        cout << "Outer ..." << endl;
        cout << e.what() << endl;
    }
}

这里写图片描述

栈展开

这里写图片描述

#include<iostream>

using namespace std;

class MyException
{
public:
    MyException(const char* message)
        :message_(message)
    {
        cout << "MyException ..." << endl;
    }
    MyException(const MyException& other) : message_(other.message_)
    {
        cout << "Copy MyException ..." << endl;
    }
    ~MyException()
    {
        cout << "~MyException" << endl;
    }
    const char* what() const
    {
        return message_.c_str();
    }
private:
    string message_;
};

class Test
{
public:
    Test()
    {
        cout << "Test ..." << endl;
    }
    Test(const Test& other)
    {
        cout << "Copy Test ..." << endl;
    }
    ~Test()
    {
        cout << "~Test ..." << endl;
    }
};
class Test3
{
public:
    Test3()
    {
        cout << "Test3 ..." << endl;
    }
    Test3(const Test3& other)
    {
        cout << "Copy Test3 ..." << endl;
    }
    ~Test3()
    {
        cout << "~Test3 ..." << endl;
        throw 4;//不能在析构函数中抛出异常
    }
};

class Obj
{
public:
    Obj()
    {
        cout << "Obj ..." << endl;
    }
    Obj(const Obj& other)
    {
        cout << "Copy Obj ..." << endl;
    }
    ~Obj()
    {
        cout << "~Obj ..." << endl;
    }
};
class Test2
{
public:
    Test2()
    {
        obj_ = new Obj;
        cout << "Test2 ..." << endl;
        throw MyException("test exception");//可以在构造函数中抛出异常,抛出异常后不会调用析构函数
    }
    Test2(const Test2& other)
    {
        cout << "Copy Test2 ..." << endl;
    }
    ~Test2()
    {
        delete obj_;
        cout << "~Test2 ..." << endl;
    }
private:
    Obj* obj_;
};


int main(void)
{
    try
    {
        /*Test t;
        throw MyException("test exception");*/

        //Test2 t2;
        Test3 t3;
        throw MyException("Test exception3");
    }
    catch (MyException& e)
    {
        cout << e.what() << endl;       
    }
    catch (int)
    {
        cout << "catch a int esception" << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值