错误处理的处理 -- <Programming Language Concepts>note

来源:互联网 发布:微信视频制作软件 编辑:程序博客网 时间:2024/06/05 08:51


程序在执行中可能出错,出错后的处理方法大体有两种:使用返回值(比如C语言),使用异常处理(Java C++ Python Ruby等)。


返回值的方法可能存在问题: 错误遗漏;干扰主流程影响代码阅读;

对于问题2,VB的处理是集中错误处理;对应引发的问题是:debug不便,对于异常无法快速定位。


使用异常处理的好处是,不会遗漏异常。但当函数存在多个出口时,必须成对处理的操作很难处理异常。

谷歌在其编程守则Google C++ Style Guide  中禁止异常使用。

Exceptions

We do not use C++ exceptions.

Decision:

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.


两种方法各有优劣。


0 0
原创粉丝点击