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

本文探讨了程序错误处理的两种主要方式:使用返回值(如C语言)和使用异常处理(如Java、C++、Python)。分析了两种方法各自的优缺点,并提及了谷歌在其编程规范中对于异常使用的立场。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


程序在执行中可能出错,出错后的处理方法大体有两种:使用返回值(比如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.


两种方法各有优劣。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值