C++核心准则E.26:如果无法抛出异常,尽快进行失败处理
E.26: If you can't throw exceptions, consider failing fast
E.26:如果无法抛出异常,尽快进行失败处理
Reason(原因)
If you can't do a good job at recovering, at least you can get out before too much consequential damage is done.
如果你无法很好的从错误中恢复,至少你可以在更多危害发生之前退出。
See also: Simulating RAII
Note(注意)
If you cannot be systematic about error handling, consider "crashing" as a response to any error that cannot be handled locally. That is, if you cannot recover from an error in the context of the function that detected it, call abort(), quick_exit(), or a similar function that will trigger some sort of system restart.
如果你不能进行系统化的错误处理,可以将”失败“视为任何无法局部处理的错误的反应。也就是说,如果你无法在检出问题的函数上下文中从错误中恢复,可以调用about函数,quick_exit()函数或者类似的可以触发某种系统重启的函数。
In systems where you have lots of processes and/or lots of computers, you need to expect and handle fatal crashes anyway, say from hardware failures. In such cases, "crashing" is simply leaving error handling to the next level of the system.
在包含很多任务或者大量计算机的系统中,反正你已经需要预估和处理(包括硬件错误的)致命失败。在这样的情况下,”失败“仅仅是将错误处理转交给系统的下一层。
Example(示例)
void f(int n)
{
// ...
p = static_cast<X*>(malloc(n * sizeof(X)));
if (!p) abort(); // abort if memory is exhausted
// ...
}
Most programs cannot handle memory exhaustion gracefully anyway. This is roughly equivalent to
大多数程序都无法满意的处理内存枯竭。这差不多和下面的代码等价:
void f(int n)
{
// ...
p = new X[n]; // throw if memory is exhausted (by default, terminate)
// ...
}
Typically, it is a good idea to log the reason for the "crash" before exiting.
通常,在因为”失败“退出之前记录原因是好想法。
Enforcement(实施建议)
Awkward
不容易
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e26-if-you-cant-throw-exceptions-consider-failing-fast
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 云主机装ikuai
- 【Vue.js】Vue.js组件库Element中的导航菜单、标签页、面包屑和页头
- CentOS7查看硬盘是SSD还是HDD
- 浅谈CentOS防火墙命令
- 【Vue.js】Vue.js组件库Element中的下拉菜单、步骤条、对话框和文字提示
- Win10触控板多指触控、手势操作教程
- 【Vue.js】Vue.js组件库Element中的弹出框、气泡确认框、卡片和走马灯
- 【Vue.js】Vue.js组件库Element中的折叠面板、时间线、分割线和日历
- 从Properties乱码来学习编码
- 【Vue.js】Vue.js组件库Element中的图片、回到顶部、无限滚动和抽屉
- Java逐行读取和写入文件
- Vuejs使用v-for指令实现九九乘法表
- Cypress系列(43)- visit() 命令详解
- 在GitLab pages上快速搭建Jekyll博客
- Dubbo项目中No provider available for the service xxx from registry xxx on the consumer问题的解决思路