.NET中的无效或意外参数应该引发什么例外? [英] What exceptions should be thrown for invalid or unexpected parameters in .NET?

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

问题描述

对于.NET中的无效或意外参数,应该抛出什么类型的异常?我什么时候选择一个而不是另一个?



跟进:



如果您有一个函数期望一个与月,你通过'42'?即使它不是一个集合,这将落入超出范围类别?

解决方案

我喜欢使用: ArgumentException ArgumentNullException ArgumentOutOfRangeException





还有其他选项也不是那么关注论证本身,而是判断e调用整体:





诀窍是抛出异常最好表达为什么方法不能被称之为它的方式。理想情况下,应该详细说明出错的原因,为什么出错,以及如何解决这个问题。



我喜欢当错误信息指向帮助,文档或其他资源。例如,Microsoft对其知识库文章进行了良好的第一步,例如。 为什么我在Internet Explorer中访问网页时收到操作中止错误消息?。当您遇到错误时,他们会指向错误消息中的知识库文章。他们做的不好就是他们不会告诉你,为什么具体失败了。



感谢STW(再见)对于意见。






为了回应你的跟进,我会抛出一个href =http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception(loband).aspx =noreferrer> ArgumentOutOfRangeException 。看看MSDN对这个例外的看法:


ArgumentOutOfRangeException 被抛出
当调用一个方法时,至少
传递给
方法的一个参数不为空
reference( Nothing in Visual基本)
并且不包含有效的值。


所以,在这种情况下,你传递一个值,但是这不是有效的值,因为您的范围是1-12。然而,您记录的方式使其清楚,您的API是什么。因为虽然我可能会说 ArgumentOutOfRangeException ,另一个开发人员可能会说 ArgumentException 。轻松地记录行为。


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

Follow-up:

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?

解决方案

I like to use: ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException.

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.

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.

Thanks to STW (ex Yoooder) again for the comments.


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

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.

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天全站免登陆