异常或错误代码 [英] Exceptions or error codes

查看:94
本文介绍了异常或错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我和同事们进行了激烈的辩论,就是首选错误报告方式。主要是讨论使用异常或错误代码来报告应用程序层或模块之间的错误。

Yesterday I was having a heated debate with a coworker on what would be the preferred error reporting method. Mainly we were discussing the usage of exceptions or error codes for reporting errors between application layers or modules.

你使用什么规则来决定是否抛出异常返回错误报告错误代码?

推荐答案

我通常喜欢异常,因为它们有更多的上下文信息,当正确使用时)错误给程序员更清晰的方式。

I normally prefer exceptions because they have more contextual information and can convey (when properly used) the error to the programmer in a more clear fashion.

另一方面,错误代码比异常更轻巧,但更难维护,错误检查可以不可忽视的它们更难维护,因为您必须保留带有所有错误代码的目录,然后打开结果以查看是否引发了什么错误。错误范围可以在这里有所帮助,因为如果我们感兴趣的唯一的事情是,如果我们存在错误,则更容易检查(HRESULT错误代码大于或等于0是成功的,小于零是失败)。他们可以无视地被忽略,因为没有编程强制,开发人员将检查错误代码。另一方面,您不能忽略例外。

On the other hand error codes are more lightweight than exceptions but are harder to maintain and error checking can inadvertedly be omitted. They are harder to maintain because you have to keep a catalog with all error codes and then switch on the result to see what error was thrown. Error ranges can be of help here because if the only thing we are interested in is if we are in the presence of an error or not is simpler to check (with an HRESULT error code greater or equal to 0 is success and less than zero is failure). They can inadvertedly be ommitted because there is no programmatic forcing that the developer will check for error codes. On the other hand you cannot ignore exceptions.

要恢复,我喜欢在几乎所有情况下的错误代码中的异常。

To resume I prefer exceptions over error codes in almost all situations.

这篇关于异常或错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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