从扩展方法ArgumentNullException或NullReferenceException异常?异常、方法、ArgumentNullException、NullReferenceExcepti

2023-09-02 21:08:26 作者:烽烟乱世遇佳人

你会认为是时候扩展方法上调用空实例(其中扩展方法不允许它)扔最好的异常类型?由于扩展方法是什么,但静态方法,你可以争辩说,它应该是ArgumentNullException,但另一方面,他们已经习惯像实例方法,所以它可能是更自然的使用NullReferenceException异常。让我们看看下面的例子:

What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example:

public static string ToInvariantString(this IFormattable value, string format)
{
    return value.ToString(format, CultureInfo.InvariantCulture);
}

这样,如果value参数为null一个NullReferenceException将被抛出。

This way a NullReferenceException will be thrown if the value parameter is null.

另一个例子是:

public static string ToInvariantString(this IFormattable value, string format)
{
    if (value == null) throw new ArgumentNullException("value");
    return value.ToString(format, CultureInfo.InvariantCulture);
}

编辑: 在一些你已经指出,一个扩展方法可以被称为像一个静态方法,并在这些情况下,一个空引用异常将是错误的答案,这是一个很大的问题,而我所关注的实际的,不知道为什么,我忘了更何况,在摆在首位的问题。

In some of the answers you have pointed out that an extension methods can be called like a static method and in those cases a null reference exception would be wrong, which is a great point, and actually one of my concerns, not sure why I forgot to mention that in the question in the first place.

也有人指出,这是错误抛出一个NullReferenceException,是的,就是这样。这就是为什么我不把它,我就让它发生(让CLR扔了吧)不守着方法。

Someone also pointed out that it's wrong to throw a NullReferenceException, and yes, it is. That's why I don't throw it, I just let it happen (let the CLR throw it) by not guarding the method.

我觉得我赞成ArgumentNullException(也就是我用至今),但我仍然认为至少有余地来争取一个对的NullReferenceException,因为它似乎在大多数地方更自然,其中的方法将是用了。

I think I favor the ArgumentNullException (that's what I've use so far) but I still think there is at least room to argue for an against the NullReferenceException since it seems more natural in most places where the method is going to be used.

推荐答案

在一般情况下,例外情况包括在内,你应该把一个扩展方法,就好像它是一个正常的静态方法。在这种情况下,你应该抛出一个ArgumentNullException。

In general, exceptions included, you should treat an extension method as if it were a normal static method. In this case you should throw an ArgumentNullException.

在这里扔一个NullReferenceException是有几个原因一个坏主意

Throwing a NullReferenceException here is a bad idea for a few reasons

在空引用实际上并没有发生这样看到的是违反直觉的 在抛出一个NullReferenceException,并导致一个NullReferenceException发生生产discernably不同的异常(单程看不同的是错误code)。这是由CLR抛出许多例外的真。

请参阅后我做了这个主题:http://blogs.msdn.com/jaredpar/archive/2008/10/22/when-can-you-catch-a-stackoverflowexception.aspx

See a post I did on this subject: http://blogs.msdn.com/jaredpar/archive/2008/10/22/when-can-you-catch-a-stackoverflowexception.aspx

这是prefectly法律调用扩展方法,就好像它是一个普通的方法。在这种情况下,我肯定不除一个NullReferenceException,而是一个ArgumentNullException。
 
精彩推荐
图片推荐