异常真的是异常错误吗? [英] Are exceptions really for exceptional errors?

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

问题描述

这是我的理解,普通的智慧说,只有使用异常才能真正有特殊的条件(实际上,我已经在这里看过这个声明了几遍)。

It's my understanding that common wisdom says to only use exceptions for truly exceptional conditions (In fact, I've seen that statement here at SO several times).

然而,Krzysztof Cwalina说:

However, Krzysztof Cwalina says:



关于异常的最大误解之一是它们是异常条件。实际情况是它们用于传达错误条件。从框架设计的角度来看,没有像特殊条件这样的东西。条件是否异常取决于使用的上下文,但可重用的库很少知道如何使用它们。例如,OutOfMemoryException对于简单的数据输入应用可能是例外;对于执行自己的内存管理的应用程序(例如SQL Server)来说,这不是特别之处。换句话说,一个人的特殊情况是另一个人的慢性病。

One of the biggest misconceptions about exceptions is that they are for "exceptional conditions." The reality is that they are for communicating error conditions. From a framework design perspective, there is no such thing as an "exceptional condition". Whether a condition is exceptional or not depends on the context of usage, --- but reusable libraries rarely know how they will be used. For example, OutOfMemoryException might be exceptional for a simple data entry application; it’s not so exceptional for applications doing their own memory management (e.g. SQL server). In other words, one man’s exceptional condition is another man’s chronic condition.


他然后也继续应该使用例外:

He then also goes on to say that exceptions should be used for:


  • 使用错误

  • 程序错误

  • 系统故障

考虑到Krzysztof Cwalina是MS的CLR团队的PM我问:想想他的陈述?

Considering Krzysztof Cwalina is the PM for the CLR team at MS I ask: What do you think of his statement?

推荐答案

这听起来过于简单,但我认为只要适当使用异常就有意义。在Java和Python这样的语言中,异常非常普遍,特别是在某些情况下。异常适用于您想通过代码路径冒泡的错误类型,并强制开发人员明确捕获。在我自己的编码中,我考虑在错误不能被忽略时添加异常的正确时机,或者抛出异常而不是将错误值返回给函数调用等等更简单。

This sounds over-simplistic, but I think it makes sense to simply use exceptions where they are appropriate. In languages like Java and Python, exceptions are very common, especially in certain situations. Exceptions are appropriate for the type of error you want to bubble up through a code path and force the developer to explicitly catch. In my own coding, I consider the right time to add an exception when the error either can't be ignored, or it's simply more elegant to throw an exception instead of returning an error value to a function call etc.

我可以想到的一些最适合的例外情况:

Some of the most appropriate places for exceptions that I can think of offhand:


  • NotImplementedException - 指定特定
    方法或函数不可用的非常合适的方法,而不是简单地返回而不执行
    任何操作。

  • OutOfMemory 异常 - 很难想象一种更好的方法来处理这种
    类型的错误,因为它代表了一个进程范围内的或者OS的内存分配
    的失败。当然,这是非常重要的!

  • NullPointerException - 访问一个空变量是程序员的错误,而IMO
    这是另一个好的地方强制出现一个错误,以浮动表面

  • ArrayIndexException - 在一个不可宽恕的语言,如C,缓冲区溢出
    是灾难性的。 Nicer语言可能会返回某个类型的空值,或者在
    中有些实现,甚至环绕数组。在我看来,抛出
    异常是一个更加优雅的回应。

  • NotImplementedException - very appropriate way of designating that a particular method or function isn't available, rather than simply returning without doing anything.
  • OutOfMemory exceptions - it's difficult to imagine a better way of handling this type of error, since it represents a process-wide or OS-wide memory allocation failure. This is essential to deal with, of course!
  • NullPointerException - Accessing a null variable is a programmer mistake, and IMO this is another good place to force an error to bubble to the surface
  • ArrayIndexException - In an unforgiving language like C, buffer overflows are disastrous. Nicer languages might return a null value of some type, or in some implementations, even wrap around the array. In my opinion, throwing an exception is a much more elegant response.

这绝对不是一个全面的列表,但希望可以说明这一点。使用异常,它们优雅而合乎逻辑。与编程一样,正确的工作是正确的工具是很好的建议。没有任何事情会出现异常 - 没有任何意义,但完全忽视一个强大而优雅的工具也是不明智的。

This is by no means a comprehensive list, but hopefully it illustrates the point. Use exceptions where they are elegant and logical. As always with programming, the right tool for the right job is good advice. There's no point going exception-crazy for nothing, but it's equally unwise to completely ignore a powerful and elegant tool at your disposal.

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

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