Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does FormatException not inherit from ArgumentException?

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?

like image 458
Paul Turner Avatar asked Jan 25 '10 20:01

Paul Turner


People also ask

When should you throw an ArgumentException?

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. The ParamName property identifies the invalid argument.

When should I use FormatException?

A FormatException exception can be thrown for one of the following reasons: In a call to a method that converts a string to some other data type, the string doesn't conform to the required pattern. This typically occurs when calling some methods of the Convert class and the Parse and ParseExact methods of some types.

What is FormatException C#?

FomatException is thrown when the format of an argument is invalid. Let us see an example. When we set a value other than int to int.Parse() method, then FormatException is thrown as shown below −

What is argument out of range exception?

An ArgumentOutOfRangeException exception is thrown when a method is invoked and at least one of the arguments passed to the method is not null and contains an invalid value that is not a member of the set of values expected for the argument.


2 Answers

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.

like image 165
mmx Avatar answered Oct 12 '22 05:10

mmx


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.

like image 30
Kevin Won Avatar answered Oct 12 '22 03:10

Kevin Won