为什么FormatException不继承自ArgumentException?
FormatException
不继承自 ArgumentException
是否有已知原因?无效格式似乎是参数无效的一种非常特殊的情况,类似于 ArgumentOutOfRangeException
。
该类的 MSDN 文章指出:
FormatException 当方法调用中参数的格式与相应形参类型的格式不匹配时抛出。例如,如果一个方法指定一个由两位数字和嵌入句点组成的
String
参数,则将仅包含两位数字的相应字符串参数传递给该方法将导致 FormatException抛出。
对我来说,这听起来像是 ArgumentException 或派生类的场景。
所有这一切意味着您无法处理更大的 ArgumentException
异常系列下的 FormatException
,也无法识别哪个参数导致抛出异常。
有什么理由让这个看似不合适的异常出现在它的位置吗?
Is there a known reason why FormatException
does not inherit from ArgumentException
? An invalid format would seem to be a very specific case of an argument being invalid, similar to ArgumentOutOfRangeException
.
The MSDN article for the class states:
FormatException is thrown when the format of an argument in a method invocation does not match the format of the corresponding formal parameter type. For example, if a method specifies a
String
parameter consisting of two digits with an embedded period, passing a corresponding string argument containing only two digits to that method would cause FormatException to be thrown.
Sounds like just the scenario for an ArgumentException
or deriving class to me.
All this means is that you can't deal with FormatException
under the larger ArgumentException
exception family, nor can you identify which parameter caused the exception to be thrown.
Is there any reason for this seemingly out-of-place exception to be where it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当方法的形式参数无效时,不一定会抛出
FormatException
。如果该方法正在消耗外部资源并且来自外部资源的数据的格式不合适,也可能会发生这种情况。例如,
BinaryReader.Read7BitEncodedInt
如果要从流中读取的内容不是有效的 7 位编码整数, 将抛出FormatException
。它根本不需要任何争论。另一方面,ArgumentException 仅当作为形式参数传递给方法的参数无效时才会抛出。您从 MSDN 文章中引用的描述比
FormatException
的实际情况更具限制性,应该予以澄清。FormatException
is not necessarily thrown when a formal argument of a method is invalid. It can also happen if the method is consuming an external resource and the format of the data from the external resource is inappropriate.For example,
BinaryReader.Read7BitEncodedInt
will throwFormatException
if what it's going to read from a stream is not a valid 7-bit encoded integer. It doesn't take any arguments at all.ArgumentException
, on the other hand, should only get thrown when an argument passed as a formal parameter to a method is invalid.The description you referenced from the MSDN article is more restrictive than
FormatException
really is and should be clarified.这有点傲慢:但是 Richter 在 CLR Via C#(第 432 页)中指出,这可能是因为 Exception 类层次结构在 .NET 中没有很好地实现:
这并不完全是您查询的答案,但我认为它是相关的,因为我认为它表明某些异常派生例继承它们的方式并没有真正好的理由。不幸的是,Exception 类的继承并没有经过深思熟虑。有点乱。
This is a bit snotty: but Richter in CLR Via C# (page 432) suggests that maybe it's because Exception class hierarchy wasn't implemented very well in .NET:
This isn't exactly an answer to your query, but I think it is relevant because I think it indicates that there isn't really a good reason why some exception derivatives inherit the way they do. Unfortunately, the Exception class inheritance wasn't all that well thought out & it's sort of a mess.