C&&C++异常处理(三)

来源:互联网 发布:android编程颜色表 编辑:程序博客网 时间:2024/06/03 06:51

在C++中catch语句块也可以抛出异常


为什么C++要支持catch语句块也可以抛出异常呢?

catch语句块中可以将捕获到的异常重新解释后再抛出(在大型的软件系统中我们就可以对异常做统一的规范,便于软件的开发管理)

在软件开发工程中,我们采用下面这样的方式统一异常

编程实验】C++中异常的重新解释
#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;    }}/*    假设: 当前的函数式第三方库中的函数,因此,我们无法修改源代码        函数名: 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;        }    }}int main(int argc, char *argv[]){    // Demo();        try    {        MyFunc(11);//这里会抛出异常    }    catch(const char* cs)//const char*异常被捕捉    {        cout << "Exception Info: " << cs << endl;//打印const char*信息    }        return 0;}
C++中的异常处理:
    --异常可以是自定义类类型
    --对于类类型异常的匹配原则依旧是至上而下严格匹配
    --赋值兼容性原则在异常类型匹配中依旧有用
因为赋值兼容性原则的存在:
    --匹配子类异常的catch放在上面(前面)
    --匹配父类异常的catch放在下面(后面)

  • 在软件开发工程中会定义一系列的异常类
  • 每个类代表工程中可能出现的一种异常类型
  • 代码复用时可能需要重解释不同的异常类
  • 在定义catch语句块时推荐使用引用作为参数
编程实验】类类型的异常
#include <iostream>#include <string>using namespace std;class Base{};class Exception : public Base{    int m_id;//异常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;        }    }}int main(int argc, char *argv[]){    try    {        MyFunc(11);    }    catch(const Exception& e)//捕捉自定义异常类    {        cout << "Exception Info: " << endl;//自定义异常类可以得到更多信息        cout << "   ID: " << e.id() << endl;//自定义异常类可以得到更多信息        cout << "   Description: " << e.description() << endl;//自定义异常类可以得到更多信息    }    catch(const Base& e)//越上层的抽象异常基类,越靠后    {        cout << "catch(const Base& e)" << endl;    }        return 0;}

小结:
  • catch语句可以抛出异常
  • 异常类可以是自定义类类型
  • 赋值兼容性原则在异常匹配中依然有效


原创粉丝点击