码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-02-12 22:59:49      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:log   virt   程序   cout   term   expect   调用   body   情况   

捕获指定的类型

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

#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)

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

标签:log   virt   程序   cout   term   expect   调用   body   情况   

原文地址:https://www.cnblogs.com/GyForever1004/p/8445699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!