【发布时间】:2010-10-20 22:05:38
【问题描述】:
.NET 中的无效或意外参数应引发哪些类型的异常?我什么时候会选择一个而不是另一个?
跟进:
如果你有一个函数需要一个对应于一个月的整数并且你传入了'42',你会使用哪个异常?即使它不是一个集合,它也会属于“超出范围”类别吗?
【问题讨论】:
-
虽然我同意大多数给出的答案(因为它们似乎足够合理),microsoft clearly states 在这种特定情况下应该是
ArgumentOutOfRangeException。如果链接发生变化,这里是相关部分:ArgumentOutOfRangeException when the value of an argument is outside the range of acceptable values; for example, when the value "46" is passed as the month argument during the creation of a DateTime。