zoukankan      html  css  js  c++  java
  • 外传篇1 异常处理深度解析

    1.异常的最终处理

    (1)问题:如果在main函数中抛出异常会发生什么?如果异常不处理,最后会传到哪里

          

    (2)下面的代码的输出什么?
          

    编程实验:异常的最终处理?

    #include <iostream>
    
    using namespace std;
    
    class Test 
    {
    public:
        Test() 
        {
            cout << "Test()"; 
            cout << endl;
        }
        
        ~Test() 
        {
            cout << "~Test()"; 
            cout << endl;
        }
    };
    
    int main()
    {
        static Test t;
        
        throw 1;
        
        return 0;
    }

    运行结果:不同编译器的运行结果不同

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    Test()
    terminate called after throwing an instance of 'int'
    Aborted (core dumped)

    2.结束函数terminate()

    (1)结束函数terminate():

    • 如果异常无法被处理terminate() 结束函数会被自动调用

    • 默认情况下terminate() 调用库函数 abort() 终止程序

    • abort() 函数使得程序执行异常而立即退出

    • C++支持替换默认的 terminate() 函数实现

    (2)terminate() 函数的替换:

    • 自定义一个无返回值无参数的函数

      1. 不能抛出任何异常

      2. 必须以某种方式结束当前程序

    • 调用 set_terminate() 设置自定义的结束函数

      1. 参数类型为 void (*) ()

      2. 返回值为默认的 terminate() 函数入口地址

    编程实验:自定义结束函数

    #include <iostream>
    #include <cstdlib>
    #include <exception>
    
    using namespace std;
    
    void my_terminate()
    {
        cout << "void my_terminate()" << endl;
        exit(1);
    }
    
    class Test 
    {
    public:
        Test() 
        {
            cout << "Test()"; 
            cout << endl;
        }
        
        ~Test() 
        {
            cout << "~Test()"; 
            cout << endl;
        }
    };
    
    int main()
    {
        set_terminate(my_terminate);
        
        static Test t;
        
        throw 1;
        
        return 0;
    }

    运行结果:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    Test()
    void my_terminate()
    ~Test()

    exit(1);改为abort();后的运行结果:

    示例2:自定义结束函数

    void my_terminate()
    {
        cout << "void my_terminate()" << endl;
        // exit(1);
        abort();
    }

    运行结果:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    Test()
    void my_terminate()
    Aborted (core dumped)

    abort()函数是异常终止一个程序,并且异常终止的时候不会调用任何对象的析构函数如果调用的是exit()函数,那么会确保所有的全局对象和静态局部对象的析构函数被调用

    3.面试题:如果析构函数中抛出异常会发生什么情况?

    示例:析构函数抛出异常

    #include <iostream>
    #include <cstdlib>
    #include <exception>
    
    using namespace std;
    
    void my_terminate()
    {
        cout << "void my_terminate()" << endl;
        exit(1);
    }
    
    class Test 
    {
    public:
        Test()
        {
            cout << "Test()";
            cout << endl;
        }
        
        ~Test()
        {
            cout << "~Test()";
            cout << endl;
            
            throw 2;
        }
    };
    
    int main()
    {
        set_terminate(my_terminate);
        
        static Test t;
        
        throw 1;
        
        return 0;
    }

    运行结果:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    Test()
    void my_terminate()
    ~Test()
    Aborted (core dumped)

    析构函数中不能抛出异常,可能导致 terminate() 多次调用
    不同编译器之间在默认的terminate() 函数实现上有差异

    4.小结

    • 如果异常没有被处理最后 terminate() 结束整个程序

    • terminate() 是整个程序释放系统资源的最后机会

    • 结束函数可以自定义,但不能继续抛出异常

    • 析构函数中不能抛出异常可能导致 terminate() 多次调用

  • 相关阅读:
    【cocos2d-x + Lua(2) C++和lua数据通讯之间的互调】
    【cocos2d-x + Lua(1) 绑定Lua并使用tolua++】
    【cocos2d-x 手游研发小技巧(6)聊天系统+字体高亮】
    【cocos2d-x 仙凡奇缘-网游研发(2) 角色换线系统】
    【cocos2d-x 仙凡奇缘-网游研发(1) 登录&注册】
    python数据分析---第04章 NumPy基础:数组和矢量计算
    python面向对象(一)
    python 常用模块之random,os,sys 模块
    (python数据分析)第03章 Python的数据结构、函数和文件
    python常用模块之时间模块
  • 原文地址:https://www.cnblogs.com/hoiday/p/10222467.html
Copyright © 2011-2022 走看看