zoukankan      html  css  js  c++  java
  • 外传二 函数的异常规格说明

    如何判断一个函数是否会抛出异常,以及抛出哪些异常?

    如果是第三方库函数我们看不到实现,只能看到声明,如何判断是否会抛出异常呢?

    解决方案:

    问题:

    如果抛出的异常不在异常规格列表中,会发生什么?

    示例:

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 void func() throw(int)
     6 {
     7     cout << "func()";
     8     cout << endl;
     9     
    10     throw 'c';
    11 }
    12 
    13 
    14 
    15 int main()
    16 {
    17     try 
    18     {
    19         func();
    20     } 
    21     catch(int) 
    22     {
    23         cout << "catch(int)";
    24         cout << endl;
    25     } 
    26     catch(char) 
    27     {
    28         cout << "catch(char)";
    29         cout << endl;
    30     }
    31 
    32     return 0;
    33 }

    bcc结果如下:

    linux结果如下:

    vs2010结果如下:

    vs中的处理方式是,抛出的异常被26行捕获处理了。

     三款编译器的行为不一样。

    vs编译器在这里没有支持标准C++的行为。

    自定义unexpected函数:

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <exception>
     4 
     5 using namespace std;
     6 
     7 void my_unexpected()
     8 {
     9     cout << "void my_unexpected()" << endl;
    10     exit(1);
    11     12 }
    13 
    14 void func() throw(int)
    15 {
    16     cout << "func()";
    17     cout << endl;
    18     
    19     throw 'c';
    20 }
    21 
    22 int main()
    23 {
    24     set_unexpected(my_unexpected);
    25     
    26     try 
    27     {
    28         func();
    29     } 
    30     catch(int) 
    31     {
    32         cout << "catch(int)";
    33         cout << endl;
    34     } 
    35     catch(char) 
    36     {
    37         cout << "catch(char)";
    38         cout << endl;
    39     }
    40 
    41     return 0;
    42 }

    结果如下:

    linux:

    bcc:

    vs2010:

    可以看到vs还是不遵循规范,还是按原来的方式处理。

    更改程序如下:

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <exception>
     4 
     5 using namespace std;
     6 
     7 void my_unexpected()
     8 {
     9     cout << "void my_unexpected()" << endl;
    10     // exit(1);
    11     throw 1;
    12 }
    13 
    14 void func() throw(int)
    15 {
    16     cout << "func()";
    17     cout << endl;
    18     
    19     throw 'c';
    20 }
    21 
    22 int main()
    23 {
    24     set_unexpected(my_unexpected);
    25     
    26     try 
    27     {
    28         func();
    29     } 
    30     catch(int) 
    31     {
    32         cout << "catch(int)";
    33         cout << endl;
    34     } 
    35     catch(char) 
    36     {
    37         cout << "catch(char)";
    38         cout << endl;
    39     }
    40 
    41     return 0;
    42 }

    结果:

    linux:

     在第7行的函数中,打印之后,又扔出int异常,这个异常和func的异常规格兼容,相当于func扔出了int异常,于是被第30行捕获处理。

    bcc:

    vs2010:

    可见vs依然按照原来的方式处理。

    小结:

  • 相关阅读:
    Linux filesystem
    centos 下 gradle 编译打包 apk
    python SSL 错误
    nginx 缓存配置
    nginx 反向代理配置
    redhat 网卡绑定
    磁盘阵列
    Centos 安装Django2.1
    python pyquery 基本用法
    python 爬虫之-- 正则表达式
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9607306.html
Copyright © 2011-2022 走看看