这篇文章学习C++异常处理的基础知识。看完后,还不过瘾,为什么大家在C++代码中都不用Exception?为什么C++11会引入一些变化?

为什么C++ exception handling需要unwind stack?

为什么throw会被抛弃?

接着看http://www.gotw.ca/publications/mill22.htm, 总结如下:

throw() specification 可以让程序员在自己写的函数上标注该函数是否会抛出异常, 会抛出哪些类型的异常,但是throw有如下的问题:

1. C++基于throw的异常处理部分是一个“Shadow Type System”

为什么这么说呢?throw()语法有时被认为是函数签名的一部分,而有的时候却不允许,而这没有标准规定,比如下面的例子

1
2
3
4
5
6
7
8
// Example 2(a): You can’t write an ES
// in a typedef.
//
void f() throw(A,B);
 
typedef void (*PF)() throw(A,B); // syntax error
 
PF pf = f;                       // can’t get here

在typdef时不把throw作为函数类型的一部分,但看下面的例子:

1
2
3
4
5
6
// Example 2(b): But you can if you omit
// the typedef!
//
void f() throw(A,B);
void (*pf)() throw(A,B);   // ok
pf = f;                    // ok

还有函数指针的例子:

1
2
3
4
5
6
// Example 2(c): Also kosher, low-carb,
// and fat-free.
//
void f() throw(A,B);
void (*pf)() throw(A,B,C); // ok
pf = f;                    // ok, less restrictive

还有继承的virtual 函数的例子:

1
2
3
4
5
6
7
8
9
10
11
12
// Example 2(d): And the ES in the signature
// does matter if it’s a virtual function.
//
class C
{
  virtual void f() throw(A,B); // same ES
};
 
class D : C
{
  void f(); // error, now the ES matters
};

  

  

2. 对throw语法的(错误)理解

很多人认为throw表示下面的意思:

1. Guarantee that functions will only throw listed exceptions (possibly none).
2. Enable compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown.

对第一条,看下面的代码:

1
2
3
4
5
6
// Example 1(b) reprise, and two
// potential white lies:
//
int Gunc() throw();    // will throw nothing (?)
 
int Hunc() throw(A,B); // can only throw A or B (?)

Gunc()真的不会throw任何异常吗?Hunc()真的只会抛出类型A和B的异常吗?不是的,代码复杂、调用嵌套多次后,很多时候程序员是无法准确标注这个函数会throw什么样的exception。而一旦未被标注的异常发生后,编译器也只是默默地做点事情,而这对我们的程序没有什么帮助。如果一个未被标准的异常发生后,编译器就调用std::unexpected()函数。unexpected()函数是全局的,很难对特定的exception提供很有帮助的处理,大部分情况就直接terminate,而且unexpected()函数是不会返回的,所以,这样的异常一旦发生,就等于退出程序。

对throw的理解应该换成下面的两句:

  • Guarantee Enforce at runtime that functions will only throw listed exceptions (possibly none).
  • Enable or prevent compiler optimizations based on the knowledge that only listed exceptions (possibly none) will be thrown having to check whether listed exceptions are indeed being thrown.

下面看看编译器干了什么,对下面的代码:

1
2
3
4
5
6
// Example 3(a)
//
int Hunc() throw(A,B)
{
  return Junc();
}

编译器生成如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example 3(b): A compiler’s massaged
// version of Example 3(a)
//
int Hunc()
try
{
  return Junc();
}
catch( A )
{
  throw;
}
catch( B )
{
  throw;
}
catch( ... )
{
  std::unexpected(); // will not return! but
}  // might throw an A or a B if you’re lucky

可见,编译器并不是根据listed exception做优化,编译器需要生成更多的代码来保证在运行时只有listed exception被throw出来,而没有list就调用unexpected函数了。

在回头看对throw的正确的理解:

1. 保证运行时,只会throw listed exception,而如果发生不listed 的exception,那就调用unexpected;

2. 允许或者禁止编译器不得不进行是否listed exception发生的检查。  

在上面说明了使用throw时,编译器需要生成try-catch代码, 其实throw还有其他问题:1. 有些编译器会自动拒绝为使用throw的function做inline优化;2. 有些编译器不回去对与exception有关的知识进行优化,即使有些代码绝不会throw exception,但编译器还是会生成try-catch代码。3. throw在virtual函数中时signature的一部分,所以,如果你把base class 的virtual方法中throw的类型之一或者若干个去掉了,那就也需要更新子类的代码,这样实际上增加了coupling,是很不好的设计。

所以关于throw,建议是

Moral #1: Never write an exception specification.

Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.

现在大家都做cross-platform的开发,多一事不如少一事,throw不会带来什么好处,所以,就完全不要用了。

既然throw有如此多的问题,那C++11带来了什么呢?

Reference:

1. http://www.learncpp.com/cpp-tutorial/153-exceptions-functions-and-stack-unwinding/

2. http://www.gotw.ca/publications/mill22.htm

3. http://stackoverflow.com/questions/88573/should-i-use-an-exception-specifier-in-c/88905#88905

4. http://stackoverflow.com/questions/10787766/when-should-i-really-use-noexcept

原文转自:http://www.cnblogs.com/whyandinside/p/3677589.html

原作者为:做个不善的人.  请尊重原作者版权

C++的异常处理之一:throw是个一无是处的东西的更多相关文章

  1. C++异常处理assert,throw,exit用法

    常见的几个小细节问题. assert应用: 在现实世界中,我们脑袋时刻都在判断对与错,对的事情我们会继续深入下去,而错的事情我们会马上停止,那么在编程开发中我们如何赋予程序这种判断事物对错的能力呢?其 ...

  2. java异常处理之throw, throws,try和catch

    转自 http://blog.csdn.net/zhouyong80/article/details/1907799  程序运行过程中可能会出现异常情况,比如被0除.对负数计算平方根等,还有可能会出现 ...

  3. 异常处理(throw,throws,try,catch,finally)

    一.异常 1.定义:程序在运行时出现的不正确的情况. 2.由来:问题也是生活中的事物,也可以被Java描述,并被封装成对象. 其实就是Java对不正常情况进行描述后的对象体现. 3.划分:Java对于 ...

  4. java异常处理的throw和throws的区别

    1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...

  5. 六. 异常处理7.throw:异常的抛出

    到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下:    throw ThrowableInstance;这里,Thr ...

  6. Swift异常处理:throw和rethrow

    Swift异常处理体现了函数式语言的特性.因此我们能够传一个会抛出异常的函数闭包(高阶函数)作为參数传到还有一个函数中(父函数),父函数能够在子函数抛出异常时直接向上抛出异常,这时用rethrowke ...

  7. java中异常处理机制 throw抛出自定义业务逻辑异常 throws继续抛出 catch捕获后会自动继续抛向调用方法

    package com.swift; public class Exception_TestC { public static void main(String[] args) { /* * 第5题: ...

  8. java异常处理机制throw

  9. C++11异常处理 noexcept

    1.简介 在C语言中,如果程序的运行出现异常.错误,我们想提供方案处理这些异常时,我们面临许多问题,如: (1)C语言没有提供统一(标准)的方式来处理错误: (2)无法保证错误会被正确的处理: (3) ...

随机推荐

  1. Eclipse常用的十个方便的快捷键

    Ctrl+F:在当前代在cg中查找关键字 Ctrl+H:打开查找窗口 Ctrl+/: 屏蔽代码(注释): (以下转自:http://wenku.baidu.com/view/d291ade3172de ...

  2. C语言中链表任意位置怎么插入数据?然后写入文件中?

    链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便. 插入某个学号后面图: 定义的结构体: struct student { ]; //学生学号 ]; //学生姓名 ...

  3. How to stop uwsgi when no pidfile in config?

    原文: how-to-stop-uwsgi-when-no-pidfile-in-config Q: I've just installed uwsgi by pip install uwsgi in ...

  4. Android中关于Handler的若干思考

    在之前的博文中,讲过一些和Handler有关的知识,例如: Android 多线程----AsyncTask异步任务详解 Android多线程----异步消息处理机制之Handler详解 今天再把Ha ...

  5. USACO(含training section)水题合集[5/未完待续]

    (1) USACO2.1 Ordered Fractions 枚举 排序即可,注意1/1 #include<iostream> #include<cstdio> #includ ...

  6. 穷举、迭代、以及while代替for循环的使用

    for循环的穷举: 就是所有情况走一遍,使用if筛选出符合的情况. while循环分为2个格式 (1)先判断再做while(){}(2)不管对错,先做了在判断do{}whlie() 百鸡百钱的whil ...

  7. 嵌入式Linux驱动学习之路(二)u-boot体验

    u-boot工程简介 现在的u-boot支持PowerPC.ARM.X86.MIPS体系结构的上百种开发板,已经称为功能最多.灵活性最强,并且开发最积极的开源Bootloader.目前由DENX的Wo ...

  8. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  9. 小Y的棋盘问题 题解

    有一个n*m的棋盘,上面有一些棋子,每行每列最多只会有一个棋子,不会有两个棋子八连通.问随机一个空格子作为起点,再随机地选择一个空格子作为终点,求问不经过任意棋子最短路的期望长度是多少.多组,n,m& ...

  10. 批量去除Teleport Pro整站下载文件冗余代码

    teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...