来自扩展方法的 ArgumentNullException 或 NullReferenceException? [英] ArgumentNullException or NullReferenceException from extension method?

查看:17
本文介绍了来自扩展方法的 ArgumentNullException 或 NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在空实例上调用扩展方法时(扩展方法不允许),您认为抛出的最佳异常类型是什么?由于扩展方法只不过是静态方法,您可能会争辩说它应该是 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 发生会产生明显不同的异常(查看差异的一种方法是错误代码).CLR 引发的许多异常都是如此.

参见 什么时候可以你发现了一个 StackOverflowException(我在这个主题上写的一篇文章).

See When can you catch a StackOverflowException (a post I did on this subject).

  • 像调用常规方法一样调用扩展方法是完全合法的.在那种情况下,我当然不会除了 NullReferenceException,而是一个 ArgumentNullException.

这篇关于来自扩展方法的 ArgumentNullException 或 NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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