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 ...
随机推荐
- cvte 面试实习经历
1.cvte招聘流程 我报的是Web后台开发的岗位,先是在线的笔试,笔试完了是2轮的技术面+1hr面试.之后考核一周,给作业考核.最后是终期任务完成情况的汇报和hr谈话确定你的岗位情况. 2.笔试 笔 ...
- container error log
learn from error- Error: org.apache.hadoop.mapreduce.task.reduce.Shuffle$ShuffleError: error in shuf ...
- jquery常用方法
一.多个按钮绑定同一事件 $("#index_svip,#index_svip_renew").click(function() { seajs.use(['svipLayer'] ...
- 正弦 sin 余弦 cos
正弦 以下概念需掌握 直角,锐角 sinA = 对边 / 斜边 性質 奇偶性 奇 定義域 (-∞,∞) 到達域 [-1,1] 周期 2π 在數學中,正弦是一種週期函數,是三角函数的一種.它的定义域是整 ...
- Flex编译程序出现 Could not find compiled resource bundle 'SharedResources' for locale 'en_US'.
Flex编译程序出现 Could not find compiled resource bundle 'SharedResources' for locale 'en_US'. 而且静态类居然为nul ...
- [AIR] as3 之条件编译多平台妙用
http://bbs.9ria.com/thread-418864-1-1.html 一直希望as3 可以支持条件编译,即满足A时编译函数1,满足B时则编译函数2. 最佳百度了之后,发现原来是可以实现 ...
- Hibernate中saveOrUpdate()和merge()的区别
this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); saveOrUpdate(): saveOrUpdate()基本上 ...
- luogu[1279]字串距离
题目描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串“abcb□cd”,“□a□bcbcd□”和“abcb□cd□”都是X ...
- Web API 接口
Web API 接口 在给网站编写 JavaScript 代码时,也有很多可用的 API.您可以使用下面的接口(也称为对象的类型)列表,开发 Web 应用程序或网站. 关于包含这些接口的 API 列表 ...
- Centos下Tomcat 安装Apache Portable Runtime
APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTTP Server 2.x的核心. APR有很多用途,包括访问高级IO功能(例如sendfile,epo ...