“我们不使用C ++异常” - 有什么办法?让它崩溃? [英] "We do not use C++ exceptions" — What's the alternative? Let it crash?

查看:145
本文介绍了“我们不使用C ++异常” - 有什么办法?让它崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们不使用C ++异常 / p>

如果不使用异常,当发生错误时会发生什么?你只是让程序崩溃?

解决方案

或者你可以再读一点:


在他们的脸上,使用
异常的好处胜过成本,特别是在新项目中的
。然而,对于现有代码,
引入
异常对所有
相关代码有影响。如果异常可以超出一个新项目的
,那么
也将成为将
新项目集成到现有的
无异常代码中的问题。因为Google的大多数
现有的C ++代码不是
准备处理异常,所以
比较难采用
生成异常的新代码。



鉴于Google的现有代码是
不是例外容忍的,使用例外的
的成本比新项目中的成本要高一些。
转换过程会很慢
,容易出错。我们不相信
可用的替代
异常,例如错误代码和
断言,引入一个重要的
负担。



我们对使用异常的建议是
不是基于哲学或
的道德理由,而是实际的。
因为我们想使用我们的
开源项目在Google和
,如果这些
项目使用异常,很难这样做,我们需要
建议反对Google
开源项目中的例外。如果我们从
再次从头开始,
可能会有所不同。



这个规则有一个例外(no
双关)Windows代码。



"We do not use C++ exceptions."

If you don't use exceptions, what happens when there's an error? You just let the program crash?

解决方案

Or you could read a little further:

On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. If exceptions can be propagated beyond a new project, it also becomes problematic to integrate the new project into existing exception-free code. Because most existing C++ code at Google is not prepared to deal with exceptions, it is comparatively difficult to adopt new code that generates exceptions.

Given that Google's existing code is not exception-tolerant, the costs of using exceptions are somewhat greater than the costs in in a new project. The conversion process would be slow and error-prone. We don't believe that the available alternatives to exceptions, such as error codes and assertions, introduce a significant burden.

Our advice against using exceptions is not predicated on philosophical or moral grounds, but practical ones. Because we'd like to use our open-source projects at Google and it's difficult to do so if those projects use exceptions, we need to advise against exceptions in Google open-source projects as well. Things would probably be different if we had to do it all over again from scratch.

There is an exception to this rule (no pun intended) for Windows code.

这篇关于“我们不使用C ++异常” - 有什么办法?让它崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆