专栏首页C++核心准则原文翻译C++核心准则E.26:如果无法抛出异常,尽快进行失败处理​

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

参见:模仿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

本文分享自微信公众号 - 面向对象思考(OOThinkingDalian),作者:面向对象思考

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-08-08

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好

    Readability. Direct expression of an idea.

    面向对象思考
  • C++核心准则C.8:存在非公有成员时,使用class而不是struct定义类

    Readability. To make it clear that something is being hidden/abstracted. This is...

    面向对象思考
  • C++核心准则CP.4:按照任务思考问题,而不是线程

    CP.4: Think in terms of tasks, rather than threads

    面向对象思考
  • SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

    According to SAP help, “Smart controls are a specific category of SAPUI5 control...

    Jerry Wang
  • Educational Codeforces Round 44 (Rated for Div. 2) B. Switches and Lamps

    You are given n switches and m lamps. The i-th switch turns on some subset of th...

    用户2965768
  • Android系统启动——5 zyogte进程(Java篇)

    上一篇文章,我们知道在AndroidRuntime.cpp的start()函数里面是调用的Zygoteinit类的main()函数,那我们就继续研究

    隔壁老李头
  • Holiday -- hack the box

    Holiday is an insane box officially. It's really difficult to get the user permi...

    madneal
  • Android系统启动之zyogte进程java(下)

    ZygoteServer的代码路径frameworks/base/core/java/com/android/internal/os/ZygoteServer....

    李小白是一只喵
  • Bashed -- hack the box

    Only port 80 is open, it may be an easy box. And the truth is that it is really ...

    madneal
  • Spring AOP 失效的真正元凶

    Understanding AOP proxies Spring AOP is proxy-based. It is vitally important tha...

    Java高级架构

扫码关注云+社区

领取腾讯云代金券