C++之父Bjarne Stroustrup提供的关于异常处理的建议
节选自《The C++ Programming Language》 ——C++之父Bjarne Stroustrup
1. Don’t use exceptions where more local control structures will suffice; 当局部的控制能够处理时,不要使用异常;
2. Use the "resource allocation is initialization" technique to manage resources; 使用“资源分配即初始化”技术去管理资源;
3. Minimize the use of try-blocks. Use "resource acquisition is initialization" instead of explicit handler code; 尽量少用try-catch语句块,而是使用“资源分配即初始化”技术。
4. Throw an exception to indicate failure in a constructor; 如果构造函数内发生错误,通过抛出异常来指明。
5. Avoid throwing exceptions from destructors; 避免在析构函数中抛出异常。
6. Keep ordinary code and error-handling code separate; 保持普通程序代码和异常处理代码分开。
7. Beware of memory leaks caused by memory allocated by new not being released in case of an exception; 小心通过new分配的内存在发生异常时,可能造成内存泄露。
8. Assume that every exception that can be thrown by a function will be thrown; 如果一个函数可能抛出某种异常,那么我们调用它时,就要假定它一定会抛出该异常,即要进行处理。
9. Don't assume that every exception is derived from class exception; 要记住,不是所有的异常都继承自exception类。
10. A library shouldn't unilaterally terminate a program. Instead, throw an exception and let a caller decide; 编写的供别人调用的程序库,不应该结束程序,而应该通过抛出异常,让调用者决定如何处理(因为调用者必须要处理抛出的异常)。
11. Develop an error-handling strategy early in a design; 若开发一个项目,那么在设计阶段就要确定“错误处理的策略”。
C++之父Bjarne Stroustrup提供的关于异常处理的建议的更多相关文章
- 向C++之父Bjarne Stroustrup致敬
2013-04-25 21:30 (分类:社会人生) 非常好的文章 C ++ 的 背 影 ——C++之父Bjarne Strou ...
- What books does Bjarne Stroustrup suggest to master C++?
QUESTION : What books does Bjarne Stroustrup suggest to master C++? ANSWER: A Tour of C++ is a quick ...
- Bjarne Stroustrup announces C++ Core Guidelines
This morning in his opening keynote at CppCon, Bjarne Stroustrup announced the C++ Core Guidelines ( ...
- Bjarne Stroustrup对C++程序员的忠告
转自:http://blog.csdn.net/adm_qxx/archive/2007/05/20/1617488.aspx 第1章 致读者 [1] 在编写程序时,你是在为你针对某个问题的解决方 ...
- Bjarne Stroustrup 语录1
1. 请谈谈C++书. 没有,也不可能有一本书对于所有人来说都是最好的.不过对于那些真正的程序员来说,如果他喜欢从“经典风格”的书中间学习一些新的概念和技术,我推荐我的The C++ Program ...
- Bjarne Stroustrup语录2
一.致读者 1. 在编程序时,你是在为你针对某个问题的解决方案中的思想建立起一种具体表示.让程序的结构尽可能地直接反映这些思想: ★.如果你能把“它”看成一个独立的概念,就把它做成一个类. ...
- The C++ Programming Language - Bjarne Stroustrup
Preface Part 1: Introduction 1.1 The Structure of This Book 1.1.1 Introduction 1.1.2 Basic Facilitie ...
- Bjarne Stroustrup语录2(一些C++使用注意点)
一.致读者 1. 在编程序时,你是在为你针对某个问题的解决方案中的思想建立起一种具体表示.让程序的结构尽可能地直接反映这些思想: ★.如果你能把“它”看成一个独立的概念,就把它做成一个类. ...
- 首个 C++ 编译器诞生 30 周年了,来听听 C++ 之父畅谈 C++
原文 http://www.iteye.com/news/31076 C++ 之父 Bjarne Stroustrup 在 cfront 诞生 30 周年的访谈. 整整30年前,CFront 1 ...
随机推荐
- mysql连接报错 Host ‘xxx’is blocked because of many connection errors;unblock with 'mysqladmin flush-hosts'
程序无法连接MySQL,提示: null, message from server: "Host '192.168.6.68' is blocked because of many con ...
- [转]nopcommerce商城系统--如何编写一个插件
本文转自:http://www.cnblogs.com/ganqiyin/p/3680771.html 原址:http://www.nopcommerce.com/docs/77/how-to-wri ...
- [转]ORACLE DBA TRANSACTIONS
本文转自:http://blog.sina.com.cn/s/blog_66f845010100qelf.html 一, Transaction control 默认Transaction 由修改数据 ...
- 怎样运用好ZBrush中的布尔运算
我们知道DynaMesh常用于基础模型的起稿到中模的制作,它是ZBrush ® 4R2新增的功能,其强大的功能除了对模型进行重新布线,还可以进行布尔运算.配合Insert笔刷进行布尔运算,可以做出Z ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- java编程思想读书笔记三(11-21)
十一:持有对象 >持有对象实例 ●数组将数字与对象联系起来.它保存类型明确的对象,查询对象时,不需要对结果做类型转换.他可以是多维的. 可以保存基本的数据类型.但是,数组一旦生成,容量就不会在变 ...
- Maven系列三Maven内置变量
Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...
- noip2016代码
---------------------------------------------------------------------------------- 以下均为AC代码 -------- ...
- ASP.NET MVC铵钮Click后下载文件
本篇Insus.NET练习的是FilePathResult和FileStreamResult操作.本篇也算是<如何把Json格式字符写进text文件中>http://www.cnblogs ...
- noi1696 逆波兰表达式
1696:逆波兰表达式 http://noi.openjudge.cn/ch0303/1696/ 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算符前置的算术 ...