为什么FormatException不继承自ArgumentException?

发布于 2024-08-19 07:10:19 字数 671 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知你几分 2024-08-26 07:10:19

当方法的形式参数无效时,不一定会抛出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 throw FormatException 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.

沦落红尘 2024-08-26 07:10:19

这有点傲慢:但是 Richter 在 CLR Via C#(第 432 页)中指出,这可能是因为 Exception 类层次结构在 .NET 中没有很好地实现:

Microsoft 最初的想法是,System.Exception 将成为所有异常的基本类型,另外两种类型 System.SystemExceptionSystem.ApplicationException< /code> 是仅有的两种直接从 Exception 派生的类型。此外,CLR 引发的异常将从 SystemException 派生,所有应用程序引发的异常都将从 ApplicationException 派生。这样,开发人员可以编写一个 catch 块来捕获所有应用程序引发的异常。

但是,...这条规则并没有得到很好的遵循;一些异常是直接从 Exception (IsolatedStorageException) 派生的,一些 CLR 抛出的异常是从 ApplicationException 派生的......所以这一切都是一团糟,结果是 SystemExceptionApplicationException 类型根本没有特殊含义。此时,Microsoft 希望将它们从异常类层次结构中删除,但他们不能,因为这会破坏已经引用这些类型的任何代码。

这并不完全是您查询的答案,但我认为它是相关的,因为我认为它表明某些异常派生例继承它们的方式并没有真正好的理由。不幸的是,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:

Microsoft's original idea was that System.Exception would be the base type for all exceptions and that two other types, System.SystemException and System.ApplicationException would be the only two types immediately derived from Exception. Furthermore, exceptions thrown by the CLR would be derived from SystemException, and all the application-thrown exceptions would be derived from ApplicationException. This way, developers could write a catch block that catches all application-thrown exceptions.

However, ... this rule was not followed very well; some exceptions are immediate derived from Exception (IsolatedStorageException), some CLR thrown exceptions are derived form ApplicationException...So it is all a big mess, and the result is that the SystemException and ApplicationException types have no special meaning at all. At this point, Microsoft would like to remove them from the exception class hierarchy, but they can't because it would break any code that already references these types.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文