什么异常应该抛出在.NET无效的或意外的参数? [英] What exceptions should be thrown for invalid or unexpected parameters in .NET?

查看:131
本文介绍了什么异常应该抛出在.NET无效的或意外的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么类型的异常应甩在.NET无效的或意外的参数?什么时候我会选择一个,而不是另一个?

What types of exceptions should be thrown for invalid or unexpected parameters in .NET? When would I choose one instead of another?

如果你有一个函数期望对应于一个整数,你会用哪个异常一个月后,您在'42'通过呢?这会落入超出范围的范畴,即使它不是一个集合?

Which exception would you use if you have a function expecting an integer corresponding to a month and you passed in '42'? Would this fall into the "out of range" category even though it's not a collection?

推荐答案

我喜欢用:的ArgumentException ArgumentNullException ArgumentOutOfRangeException

  • ArgumentException – Something is wrong with the argument.
  • ArgumentNullException – Argument is null.
  • ArgumentOutOfRangeException – I don’t use this one much, but a common use is indexing into a collection, and giving an index which is to large.

有其他的选择,那就是,不注重参数本身这么多,而判断调用作为一个整体:

There are other options, too, that do not focus so much on the argument itself, but rather judge the call as a whole:

  • InvalidOperationException – The argument might be OK, but not in the current state of the object. Credit goes to STW (previously Yoooder). Vote his answer up as well.
  • NotSupportedException – The arguments passed in are valid, but just not supported in this implementation. Imagine an FTP client, and you pass a command in that the client doesn’t support.

关键是要抛出异常最能表达为何方法不能把它称为是这样的。理想情况下,异常应该是什么地方出了错,为什么它是错误的,如何解决它。

The trick is to throw the exception that best expresses why the method cannot be called the way it is. Ideally, the exception should be detailed about what went wrong, why it is wrong, and how to fix it.

我爱当出现错误信息指向帮助文档,或详细其他资源。例如,微软做了与他们的知识库文章,例如一个良好的开端为什么我收到操作已中止错误消息,当我访问网页在Internet Explorer?。当您遇到错误,他们指出你错误消息的知识库文章。他们没有做好的是,他们不告诉你,为什么具体地说,它失败了。

I love when error messages point to help, documentation, or other resources. For example, Microsoft did a good first step with their KB articles, e.g. "Why do I receive an "Operation aborted" error message when I visit a Web page in Internet Explorer?". When you encounter the error, they point you to the KB article in the error message. What they don’t do well is that they don’t tell you, why specifically it failed.

<子>感谢STW(前Yoooder)再次您的意见。

在回答您的随访,我会抛出一个<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception(loband).aspx> ArgumentOutOfRangeException 。再看一下MSDN说,关于此异常:

In response to your followup, I would throw an ArgumentOutOfRangeException. Look at what MSDN says about this exception:

ArgumentOutOfRangeException 被抛出
当一个方法被调用,至少
的传递到
方法的参数之一就是不为空
引用(没有在Visual基本)
,并且不包含一个有效的值。

ArgumentOutOfRangeException is thrown when a method is invoked and at least one of the arguments passed to the method is not null reference (Nothing in Visual Basic) and does not contain a valid value.

因此,在这种情况下,你传递一个值,但这不是一个有效的值,因为你的范围是1-12。但是,您记录的方式讲清楚,你的API抛出什么。因为虽然我可能会说 ArgumentOutOfRangeException ,另一个开发人员可能会说的ArgumentException 。可以很容易和文件的行为。

So, in this case, you are passing a value, but that is not a valid value, since your range is 1–12. However, the way you document it makes it clear, what your API throws. Because although I might say ArgumentOutOfRangeException, another developer might say ArgumentException. Make it easy and document the behavior.

这篇关于什么异常应该抛出在.NET无效的或意外的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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