前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​NR.3:不要拒绝使用异常

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

作者头像
面向对象思考
发布2020-11-10 11:16:59
4610
发布2020-11-10 11:16:59
举报
文章被收录于专栏:C++核心准则原文翻译

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(示例)
代码语言:javascript
复制
???
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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NR.3: Don't avoid exceptions
  • Reason(原因)
    • Example(示例)
      • Alternative(代替选项)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档