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
Stringparameter 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?
FormatExceptionis 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.Read7BitEncodedIntwill throwFormatExceptionif 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
FormatExceptionreally is and should be clarified.