手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

C++异常的几种捕获方式

[日期:2018-02-13] 来源:Linux社区  作者:GyForever1004 [字体: ]

捕获指定的类型

这样的话可以对每种异常做出不同的处理,例如:

#include <iostream>

using namespace std;

void A(int n){
    int a = 1;
    float b = 0.2;
    double c = 0.3;

    if(n == 1)
        throw a;
    else if(n == 2)
        throw b;
    else if(n == 3)
        throw c;
}

int main()
{
    int test;

    while(cin >> test){
        try{
            A(test);
        }catch(int i){
            cout << "Catch int exception " << i << endl;
        }catch(float i){
            cout << "Catch float exception " << i << endl;
        }catch(double i){
            cout << "Catch double exception " << i << endl;
        }
}

    return 0;
}

捕获泛型

如果想捕获全部类型异常的话,C++ 提供了一种简便的做法,在 catch 子句的异常声明中使用省略号来作为异常声明,例如:

void function(){  
    try {  
        /* your code */  
    }catch(...) {  
        /* handler */
    }
}

捕获类

例如:

#include <iostream>

using namespace std;

class Base{
public:
    void name(){
        cout << "My name is Base" << endl;
    }
};

void A(){
    throw Base();
}

int main()
{
    try{
        A();
    }catch(Base &e){
        e.name();
    }

    return 0;
}

也可以捕获 Base 的子类,并且在 Base 类的成员函数前加 virtual 实现多态,这样的话就可以调用子类的 name 方法,例如:

#include <iostream>

using namespace std;

class Base{
public:
    virtual void name(){
        cout << "My name is Base" << endl;
    }
};

class Derived : public Base{
public:
    void name(){
        cout << "My name is Derived" << endl;
    }
};

void A(){
    throw Derived();
}

int main()
{
    try{
        A();
    }catch(Base &e){
        e.name();
    }

    return 0;
}

捕获未期望的异常

可以在函数名后用 throw 来声明该函数可能抛出的异常,例如:

#include <iostream>

using namespace std;

void A() throw (int, float)
{
    int a = 1;
    float b = 0.2;
    double c = 0.3;

    throw c;
}

int main()
{
    try{
        A();
    }catch(...){
        cout << "Catch exception" << endl;
    }

    return 0;
}

但是,如果函数抛出的异常类型未在声明范围内的话,程序就会发生崩溃:

运行结果:

terminate called after throwing an instance of 'double'
Aborted (core dumped)

即使你使用了 catch(...) ,但它也捕获不到,异常会继续向上汇报,最终被系统默认的函数进行处理。

但我们可以使用 set_unexpected (unexpected_handler func) 这个函数来修改默认的处理方法,来实现自己的处理方式。

未实现捕获的异常

假如函数抛出一个 double 的异常,但在我们捕获的函数中没有实现 double 类型的捕获,当然也没有使用 catch(...),这种情况和未期望的异常差不多,它也会上报系统,调用系统默认的处理函数。同样我们也可以更改这个默认函数,使用如下方法:

terminate_handler set_terminate (terminate_handler func)

示例程序:

#include <iostream>

void exception(){
    std::cout << "my_terminate" << std::endl;
}

int main()
{
    std::set_terminate(exception);

    throw 0;

    return 0;
}

运行结果:

my_terminate
Aborted (core dumped)

本文永久更新链接地址http://www.linuxidc.com/Linux/2018-02/150920.htm

linux
相关资讯       C++异常 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款