zoukankan      html  css  js  c++  java
  • C++异常的几种捕获方式

    捕获指定的类型

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

    #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)
    
  • 相关阅读:
    mvc 注册脚本的其他方法
    如何将html代码转换为Xml并生成Dom树
    用TFIDF值来判断词的重要性
    oem key
    vs2010 下的ado.net 实体模型创建数据库时datetime 类型的问题
    asp.net mvc 动态注册脚本
    ASP.NET 中如何对生成的 HTML 内容流进行控制
    C# 子窗体调用父窗体的方法
    layui 弹出新页面
    winform excel导入进度条
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/8445699.html
Copyright © 2011-2022 走看看