65_C++中的异常处理(下)

1. catch语句块中可以抛出异常

  • catch中抛出的异常需要外层的try...catch...捕获

问题: 为什么要在catch中重新抛出异常?
catch中捕获的异常可以被重新解释后抛出,工程开发中使用这样的方式统一异常类型

编程说明:异常的重新解释示例1

#include <iostream>
#include <string>

using namespace std;

void Demo()
{
    try
    {
        try
        {
            throw 'c';
        }
        catch(int i)
        {
            cout << "Inner: catch(int i)" << endl;
            throw i;
        }
        catch(...)
        {
            cout << "Inner: catch(...)" << endl;
            throw ;
        }
    }
    catch(...)
    {
        cout << "outer: catch(...)" << endl;
    }
}

int main()
{
    Demo();
    
    return 0;
}

输出结果:

Inner: catch(...)
outer: catch(...)

编程说明:异常的重新解释示例2

#include <iostream>
#include <string>

using namespace std;

/*
    假设: 当前的函数是第三方库中的函数,因此,我们无法修改源代码
    
    函数名: void func(int i)
    抛出异常信息: int
                    -1 --> 参数异常
                    -2 --> 运行异常
                    -3 --> 超时异常
*/

void func(int i)
{
    if( i < 0 )
    {
        throw -1;
    }
    
    if( i > 100 )
    {
        throw -2;
    }
    
    if( i == 11 )
    {
        throw -3;
    }
    
    cout << "Run func ..." << endl;
}

void MyFunc(int i)
{
    try
    {
        func(i);
    }
    catch(int i)
    {
        switch(i)
        {
            case -1:
                throw "Invalid Parameter";
                break;
            case -2:
                throw "Runtime Exception";
                break;
            case -3:
                throw "Timeout Exception";
                break;
        }
    }
    catch(...)
    {
        throw ;
    }
}

int main()
{
    try
    {
        MyFunc(11);
    }
    catch(const char* cs)
    {
        cout << "Exception Info: " << cs << endl;
    }
    catch(...)
    {
        cout << "Unknown Exception!!!" << endl;
    }
    
    return 0;
}

输出结果

Exception Info: Timeout Exception

2. 自定义异常类

  • 异常的类型可以是自定义类类型
  • 对于类类型异常的匹配依旧是至上而下严格匹配
  • 赋值兼容性原则在异常匹配中依然适用,即子类的异常情况可以被父类的异常语句块catch到
  • 一般原则:
    • 匹配子类异常的catch放在上部
    • 匹配父类异常的catch放在下部
  • 在工程中会定义一系列的异常类
  • 每个类代表工程中可能出现的一种异常类型
  • 代码复用时可能需要重解释不同点的异常类
  • 在定义catch语句块时推荐使用引用作为参数,可以避开拷贝构造,提高程序效率

编程说明:类类型的异常

#include <iostream>
#include <string>

using namespace std;

class Base
{

};

class Exception : public Base
{
    int m_id;
    string m_desc;
public:
    Exception(int id, string desc)
    {
        m_id = id;
        m_desc = desc;
    }
    
    int id() const
    {
        return m_id;
    }
    
    string description() const
    {
        return m_desc;
    }
};


/*
    假设: 当前的函数是第三方库中的函数,因此,我们无法修改源代码
    
    函数名: void func(int i)
    抛出异常信息: int
                    -1 --> 参数异常
                    -2 --> 运行异常
                    -3 --> 超时异常
*/

void func(int i)
{
    if( i < 0 )
    {
        throw -1;
    }
    
    if( i > 100 )
    {
        throw -2;
    }
    
    if( i == 11 )
    {
        throw -3;
    }
    
    cout << "Run func ..." << endl;
}

void MyFunc(int i)
{
    try
    {
        func(i);
    }
    catch(int i)
    {
        switch(i)
        {
            case -1:
                throw Exception(-1, "Invalid Parameter");
                break;
            case -2:
                throw Exception(-2, "Runtime Exception");
                break;
            case -3:
                throw Exception(-3, "Timeout Exception");
                break;
        }
    }
    catch(...)
    {
        throw ;
    }
}

int main()
{
    try
    {
        MyFunc(11);
    }
    catch(const Exception& e)
    {
        cout << "Exception Info: " << endl;
        cout << "   Id: " << e.id() << endl;
        cout << "   Description: " << e.description() << endl;
    }
    catch(...)
    {
        cout << "Unknown Exception!!!" << endl;
    }
    
    return 0;
}

输出结果:

Exception Info: 
    Id: -3
    Description: Timeout Exception

3. STL中的异常类

  • C++标准库(STL)中提供了实用异常类族
  • SLT中的异常都是从exception类派生的
  • exception类有两个主要的分支:
    • logic_error:常用于程序中的可避免逻辑错误
    • runtime_error:常用于程序中无法避免的恶性错误

编程说明:STL中的异常使用

补充

4. 小结

  • catch语句块中可以抛出异常
  • 异常的类型可以是自定义类类型
  • 赋值兼容性原则在异常匹配中依然适用
  • 标准库中的异常都是从exception类派生的
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 八、深入理解java异常处理机制 引子try…catch…finally恐怕是大家再熟悉不过的语句了, 你的答案是...
    壹点零阅读 1,650评论 0 0
  • 六种异常处理的陋习 你觉得自己是一个Java专家吗?是否肯定自己已经全面掌握了Java的异常处理机制?在下面这段代...
    Executing阅读 1,370评论 0 6
  • 晚饭后,我闷闷不乐的收拾书桌。昨天丢了手机。 一会姥姥过来,递给我一摞毛爷爷,说,这是三千六百块钱,你被人骗的两千...
    大雨不愁阅读 348评论 1 1
  • 【花粉过敏和酵素】春暖花开[玫瑰][玫瑰],到处一片花团锦簇的美丽景色[太阳],但是却有人享受不了这样的美景,因为...
    纤修堂阅读 1,124评论 1 1
  • 晴一重,雨一重,情愫丝丝千千扣,拂晓晨光冷。 喜一程,忧一程,脉脉情思淬心头,相思明月久 。
    相思明月久阅读 304评论 2 2