c# 为什么不抛出 ArgumentNullException?
我刚刚在这里读到我不应该抛出ArgumentNullException
来自我自己的源代码。这是为什么呢?
如果我有一个不应传递 null
的方法,为什么不检查参数并抛出它是否确实为 null?
public void DoStuff(List<Int32> list) // Shouldn't be null.
{
if(list == null) // If input is null...
throw new ArgumentNullException();
...
}
谢谢。
I was just reading here that I shouldn't throw ArgumentNullException
from within my own source code. Why is this?
If I have a method that shouldn't be passed null
, why not check the parameter and throw if it is indeed null?
public void DoStuff(List<Int32> list) // Shouldn't be null.
{
if(list == null) // If input is null...
throw new ArgumentNullException();
...
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该引用没有说任何这样的事情(我找不到该页面上提到的 ArgumentNullException )。它确实说不要抛出 NullReferenceException,这是一个完全不同的异常,确实不应该从您自己的代码中抛出(您没有理由这样做)。
我总是从自己的代码中抛出 ArgumentNullException,.NET Framework 在检查参数时也是如此。
That reference doesn't say any such thing (I can't find ArgumentNullException mentioned on that page). It does say not to throw a NullReferenceException, which is a completely different exception and should indeed not be thrown from your own code (you have no reason to).
I throw ArgumentNullException from my own code all the time, and so does the .NET Framework when checking parameters.
当然你应该。事件您自己的链接将此示例作为第一个示例。如果该方法是公共的,您应该始终检查参数的有效性。
该链接仅讨论 NullReferenceException,并且仅当您尝试访问空引用对象的方法时才会发生这种情况。不抛出 NullReferenceException 是正确的,因为它们仅由运行时抛出。
Of course you should. Event your own link has this example as the first one. If the method is public you should check always the parameter validity.
The link only talks about NullReferenceException and this only happens when you try to access a method of a null reference object. It's right to not throw NullReferenceExceptions as they are thrown only by the runtime.
抛出 ArgumentNullException 没有任何问题,这是一个很好的做法。
该文章讨论的是 System.Exception、System.SystemException、System.NullReferenceException 或 System.IndexOutOfRangeException
There is nothing wrong in throwing ArgumentNullException, it is a good practice.
the article is talking about System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException
如果可以对空参数使用默认值,并且只要明确记录默认值,那么可能值得考虑该选项。
但是,您应该始终验证公共方法中的所有参数。不要相信其他开发人员知道他们在做什么。由于无效参数而引发的任何异常都应继承自
ArgumentException
类。If it is possible to use a default value for a null parameter, and as long as the default value is clearly documented, then it might be worth considering that option.
However, you should always validate any parameters in a public method. Don't trust other developers to know what they're doing. Any exceptions thrown as a result of an invalid argument should inherit from the
ArgumentException
class.因为如果您抛出 *NullReferenceException *,则意味着您预期其中一个对象可能具有对其自身的空引用。最好通过将逻辑编程到代码中来处理这个问题,而不是依赖异常来处理它。
作为普通执行的一部分,不应使用异常来更改程序流程。它们应该仅用于报告和处理错误情况。
Because if you throw a *NullReferenceException *, that means you anticipated that one of the objects would could have a null reference to itself. It would be better to handle this issue with programming the logic into your code as opposed to relying on an exception to handle it.
Exceptions should not be used to change the flow of a program as part of ordinary execution. They should only be used to report and handle error conditions.