NR.3: Don't avoid exceptions

NR.3:不要拒绝使用异常

Reason(原因)

There seem to be four main reasons given for not using exceptions:

不使用异常的四个主要原因可能是:

  • exceptions are inefficient

    例外是低效的

  • exceptions lead to leaks and errors

    异常导致泄漏和错误

  • exception performance is not predictable

    异常表现不可预测

  • the exception-handling run-time support takes up too much space

    异常处理运行时支持占用了太多空间

There is no way we can settle this issue to the satisfaction of everybody. After all, the discussions about exceptions have been going on for 40+ years. Some languages cannot be used without exceptions, but others do not support them. This leads to strong traditions for the use and non-use of exceptions, and to heated debates.

我们没有办法使这个问题能让大家都满意。毕竟,关于异常的讨论已经进行了40多年。某些语言离开例外无法使用,但其他语言则根本就不支持例外。这导致使用和不使用例外都形成了强大的传统,并引发激烈的辩论。

However, we can briefly outline why we consider exceptions the best alternative for general-purpose programming and in the context of these guidelines. Simple arguments for and against are often inconclusive. There are specialized applications where exceptions indeed can be inappropriate (e.g., hard-real-time systems without support for reliable estimates of the cost of handling an exception).

但是,我们可以简要概述为什么我们将例外视为本准则背景下,通用编程的最佳替代方法。 赞成和反对的简单议论通常是无法形成定论的。在某些特殊的应用程序中,异常确实可能是不合适的(例如,不支持可靠估计异常处理成本的硬实时系统)。

Consider the major objections to exceptions in turn

依次考虑对例外的主要反对意见

  • Exceptions are inefficient: Compared to what? When comparing make sure that the same set of errors are handled and that they are handled equivalently. In particular, do not compare a program that immediately terminate on seeing an error with a program that carefully cleans up resources before logging an error. Yes, some systems have poor exception handling implementations; sometimes, such implementations force us to use other error-handling approaches, but that's not a fundamental problem with exceptions. When using an efficiency argument - in any context - be careful that you have good data that actually provides insight into the problem under discussion.

    例外是低效的:与什么相比?比较时,请确保处理了相同的错误种类,并且对它们进行了同等的处理。 尤其是,请不要将在看到错误后立即终止的程序与在记录错误之前仔细清理资源的程序进行比较。是的,某些系统的异常处理确实差劲;有时,这样的实现迫使我们使用其他错误处理方法,但这不是例外的根本问题。在任何情况下使用效率论点时,请注意您拥有确实可以洞悉所讨论的问题的良好数据。

  • Exceptions lead to leaks and errors. They do not. If your program is a rat's nest of pointers without an overall strategy for resource management, you have a problem whatever you do. If your system consists of a million lines of such code, you probably will not be able to use exceptions, but that's a problem with excessive and undisciplined pointer use, rather than with exceptions. In our opinion, you need RAII to make exception-based error handling simple and safe -- simpler and safer than alternatives.

    异常会导致泄漏和错误。不会的。如果您的程序是一堆棘手的指针,却没有资源管理的整体策略,那么无论您怎么做,都会遇到问题。如果您的系统由一百万行这样的代码组成,您可能将无法使用异常,但这是过度和无序使用指针而不是异常的问题。我们认为,您需要RAII才能使基于异常的错误处理变得简单而又安全-与替代方法相比更加简单和安全。

  • Exception performance is not predictable. If you are in a hard-real-time system where you must guarantee completion of a task in a given time, you need tools to back up such guarantees. As far as we know such tools are not available (at least not to most programmers).

    异常性能是不可预测的。如果您在硬实时系统中,必须保证在给定时间内完成任务,则需要工具来确保实现此类保证。据我们所知,此类工具尚不可用(至少对于大多数程序员而言如此)。

  • the exception-handling run-time support takes up too much space This can be the case in small (usually embedded systems). However, before abandoning exceptions consider what space consistent error-handling using error-codes would require and what failure to catch an error would cost.

    异常处理运行时支持占用了太多空间,在小型(通常是嵌入式系统)中可能是这种情况。但是,在放弃异常之前,请考虑使用错误代码进行一致的错误处理将需要多少空间,以及捕获错误的失败将花费多少。

Many, possibly most, problems with exceptions stem from historical needs to interact with messy old code.

许多(可能是大多数)异常问题源于与混乱的旧代码进行交互的历史需求。

The fundamental arguments for the use of exceptions are

使用例外的基本论据是

  • They clearly differentiate between erroneous return and ordinary return

    它们清楚地区分错误返回和普通返回

  • They cannot be forgotten or ignored

    它们无法被忘记或忽略

  • They can be used systematically

    它们可以被系统地使用

Remember(记住)

  • Exceptions are for reporting errors (in C++; other languages can have different uses for exceptions).

    异常用于报告错误(在C ++中;其他语言对异常的用法可能不同)。

  • Exceptions are not for errors that can be handled locally.

    异常不适用于可以在本地处理的错误。

  • Don't try to catch every exception in every function (that's tedious, clumsy, and leads to slow code).

    不要试图捕获每个函数中的每个异常(这很乏味,笨拙,并且会导致低效代码)。

  • Exceptions are not for errors that require instant termination of a module/system after a non-recoverable error.

    异常不属于那些在不可恢复的错误之后需要立即终止模块/系统的错误。

Example(示例)

???

Alternative(代替选项)

  • RAII

  • Contracts/assertions: Use GSL's Expects and Ensures (until we get language support for contracts)

    契约/主张:使用GSL的期望和保证(直到我们获得合同的语言支持)

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nr3-dont-avoid-exceptions

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

C++核心准则​NR.3:不要拒绝使用异常相关推荐

  1. C++核心准则​NR.4:不要坚持将每个类声明放在其自己的源文件中

    NR.4: Don't insist on placing each class declaration in its own source file NR.4:不要坚持将每个类声明放在其自己的源文件 ...

  2. C++核心准则E.30:不要使用抛异常声明

    E.30: Don't use exception specifications E.20:不要使用抛异常声明 Reason(原因) Exception specifications make err ...

  3. php 语法 条件变量,C ++核心准则:注意条件变量的陷阱

    今天,我写了一篇关于条件变量的恐怖文章.您应该意识到条件变量的这一问题.C ++核心准则CP 42仅声明:"不要无条件等待". 等待!条件变量支持一个非常简单的概念.一个线程准备一 ...

  4. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  5. 开源压缩算法brotli_Google的Brotli压缩算法,C ++核心准则以及更多新闻

    开源压缩算法brotli 在本周的开源新闻综述中,我们将介绍Google的Brotli压缩算法,适用于GitHub的Classroom,C ++ Core Guidelines等! 2015年9月20 ...

  6. WIN7中 HttpListener 拒绝访问 异常解决 C#

    WIN7中 HttpListener 拒绝访问 异常解决 C# 参考文章: (1)WIN7中 HttpListener 拒绝访问 异常解决 C# (2)https://www.cnblogs.com/ ...

  7. C++核心准则CP.1: 设想你的代码​会成为多线程程序的一部分

    CP.1: Assume that your code will run as part of a multi-threaded program CP.1: 设想你的代码会成为多线程程序的一部分 Re ...

  8. C++核心准则C.4:只有直接访问表达的函数,才应该成为成员

    C.4: Make a function a member only if it needs direct access to the representation of a class C.4:只有 ...

  9. C++核心准则ES.56​:只在需要将一个对象显式移动到另外的作用域时使用std::move​

    ES.56: Write std::move() only when you need to explicitly move an object to another scope ES.56:只在需要 ...

最新文章

  1. hbuilderX安装git插件→拉取线上gitlab项目
  2. 一盘棋都没下过,AI只听人类评论比赛,就学会了国际象棋,还战胜了DeepChess...
  3. [转]PowerDesigner使用
  4. leader选举的源码分析-FastLeaderElection.starter
  5. openpyxl写入一行数据_老板让我从几百个Excel中查找数据,我用Python一分钟搞定!...
  6. IIS6.0,Apache低版本,PHP CGI 解析漏洞
  7. 【BZOJ1030】【Tyvj1806】文本生成器,AC自动机+DP
  8. 5个开发人员不应该错过的最好跨平台PHP编辑器
  9. Easyui datagrid getData简单使用
  10. 生物医学网络中的图嵌入:方法,应用和评估
  11. Win10怎么合并磁盘分区 合并磁盘的操作方法
  12. Cesium 影像纠偏
  13. Machine Learning-数学基础2
  14. 话题|寒窗苦读还是花花世界,你会怎么选?
  15. 黑帽SEO关键字堆砌的原理以及作用
  16. 算法 64式 4、回溯算法整理__第1部分_1到13题
  17. 什么是CHIP LAN片式网络变压器?片式网络变压器分哪几种?目前应用较多的是哪种片式结构?
  18. 高级职称17计算机,(高级职称计算机考试.doc
  19. 如何通过微信上发送邮件呢,有用过的吗?
  20. 2020东京奥运会数据集echarts可视化分析

热门文章

  1. asp毕业设计——基于vb+VB.NET+SQL Server的网上图书预约系统设计与实现(毕业论文+程序源码)——图书预约系统
  2. U盘里面有些东西显示不出来了。解决方法
  3. vue项目登录页-实现字体动画案例
  4. 这款引文数量全球的SOD检测试剂盒,你可能想不到
  5. 道氏理论:123法则与2B法则
  6. matlab 正版贵吗,有人使用正版MATLAB吗?
  7. 主流优秀BLOG程序
  8. ORA-01113,ORA-01110的简单解决
  9. SPADE(GauGAN)算法笔记
  10. python绘制星空图、要求有圆月_Python神笔马良案例集