“We do not use C++ exceptions” — What's the alternative? Let it crash?

前端 未结 7 420
南旧
南旧 2021-02-01 14:25

\"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?

7条回答
  •  一整个雨季
    2021-02-01 14:47

    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.

提交回复
热议问题