为什么不抛出这些异常? [英] Why are we not to throw these exceptions?

查看:138
本文介绍了为什么不抛出这些异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了该MSDN页面指出:


不要抛出异常 SystemException NullReferenceException IndexOutOfRangeException 故意从您自己的来源代码。

Do not throw Exception, SystemException, NullReferenceException, or IndexOutOfRangeException intentionally from your own source code.

不幸的是,它并不麻烦解释为什么。我可以猜到原因,但是我希望那些对这个问题有更多权威性的人可能会提供他们的洞察力。

Unfortunately, it does not bother to explain why. I can guess the reasons but I hope that someone more authoritative on the subject might offer their insight.

前两个有一些明显的意义,但后两个看起来像你会想雇用(事实上,我有)。

The first two make some obvious sense, but the latter two seem like ones you would want to employ (and in fact, I have).

此外,这是唯一应该避免的例外吗?如果还有其他的,他们是什么,为什么还要避免呢?

Further, are these the only exceptions one should avoid? If there are others, what are they and why should they, too, be avoided?

推荐答案

异常 是所有的基本类型例外,如此非常特别。你不应该抛出这个异常,因为它根本不包含任何有用的信息。捕获异常的调用代码无法消除有意抛出的异常(从您的逻辑)与其他完全不需要的系统异常并指出实际故障。

Exception is the base type for all exceptions, and as such terribly unspecific. You shouldn’t ever throw this exception because it simply does not contain any useful information. Calling code catching for exceptions couldn’t disambiguate the intentionally thrown exception (from your logic) from other system exceptions that are entirely undesired and point out real faults.

同样的原因也适用于 SystemException 。如果您查看派生类型的列表,您可以看到大量其他异常与非常不同的语义。

The same reason also applies to SystemException. If you look at the list of derived types, you can see a huge number of other exceptions with very different semantics.

NullReferenceException IndexOutOfRangeException 是不同的。现在这些都是非常具体的例外,所以扔掉他们可能会很好。然而,你仍然不会想要抛出这些,因为它们通常意味着你的逻辑有一些实际的错误。例如,null引用异常意味着您尝试访问一个对象的成员,该对象是 null 。如果这是代码中的一个可能性,那么你应该总是明确地检查 null ,并抛出一个更有用的异常(例如 ArgumentNullException )。同样,当您访问无效的索引(在数组上,而不是列表)时,会发生 IndexOutOfRangeException 。您应该始终确保您不要这样做,并检查例如一个数组首先。

NullReferenceException and IndexOutOfRangeException are of a different kind. Now these are very specific exceptions, so throwing them could be fine. However, you still won’t want to throw these, as they usually mean that there are some actual mistakes in your logic. For example the null reference exception means that you are trying to access a member of an object which is null. If that’s a possibility in your code, then you should always explicitly check for null and throw a more useful exception instead (for example ArgumentNullException). Similarly, IndexOutOfRangeExceptions occur when you access an invalid index (on arrays—not lists). You should always make sure that you don’t do that in the first place and check the boundaries of e.g. an array first.

还有一些其他例外,如这两个,例如 InvalidCastException DivideByZeroException ,这些错误在代码中被抛出,通常意味着你做错了,或者你没有先检查一些无效值。通过从您的代码中揣测他们,您只是使调用代码更难以确定它们是否因代码中的某些错误而被抛出,或者仅仅因为您决定在实现中重用它们。

There are a few other exceptions like those two, for example InvalidCastException or DivideByZeroException, which are thrown for specific faults in your code and usually mean that you are doing something wrong or you are not checking for some invalid values first. By throwing them knowingly from your code, you are just making it harder for the calling code to determine whether they were thrown due some fault in the code, or just because you decided to reuse them for something in your implementation.

当然,这些规则有一些例外(hah)。如果您正在构建可能导致与现有异常完全匹配的异常的东西,那么请随时使用,特别是如果您尝试匹配一些内置的行为。只要确保你选择一个非常特殊的异常类型,那么。

Of course, there are some exceptions (hah) to these rules. If you are building something that may cause an exception which exactly matches an existing one, then feel free to use that, especially if you are trying to match some built-in behavior. Just make sure you choose a very specific exception type then.

一般来说,除非你找到一个满足你需要的(特定的)异常,你应该总是考虑创建你的自己的特殊预期异常的异常类型。特别是在编写库代码的时候,这对分离异常源是非常有用的。

In general though, unless you find a (specific) exception that fills your need, you should always consider creating your own exception types for specific expected exceptions. Especially when you are writing library code, this can be very useful to separate the exception sources.

这篇关于为什么不抛出这些异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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