.Net Invalid function argument: ArgumentException or FormatException?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
10 views
We first give a formal description of the exception according to MSDN. Result in Russian, it was clear to all:

1. ArgumentException

This exception is thrown if one of the arguments provided to a method is invalid.

2. FormatException

The exception that is thrown if the format argument does not match the parameter specifications of the invoked method.


As can be seen from the official documentation — both exceptions should only occur due to incorrect value of the argument passed to the method. Because FormatException is not inherited from ArgumentException — many people believe that in the official documentation error (for 10 years no one bothered to fix).


What is there is clear criterion of when some of these two exceptions to be thrown? Here, for example,

FileInfo fileInfo = new FileInfo("*"); // ArgumentException



and

Uri Uri = new Uri("*"); // UriFormatException



or even

WebRequest.Create("*"); // UriFormatException


So what are the common standards by throwing an ArgumentException|FormatException, to avoid any confusion?


Thank you for your attention!
by | 10 views

1 Answer

0 like 0 dislike
In my opinion formatted exceptions occur in rows or when you explicitly pass an object which can have several "formats" of representation (although in this case it is better to implement the Converter as possible and not to throw acceptany at all).
If we actually gave incorrect information — ArgumentException if passed the correct data, but in the wrong format, a FormatException. Only here it is necessary to determine in the context of a particular application, whether input data to be incorrect (using the same format) or all the data is correct, but because of the different formats may fail. For example, when generating a date from a string to type string "dfdfdsfsf" I would threw an exception of the wrong format, and for data "32.09.2010" error invalid argument (generates it will be the ACC. already at a lower level, when checking dates).
by

Related questions

0 like 0 dislike
5 answers
0 like 0 dislike
3 answers
asked Mar 26, 2019 by Napseg
0 like 0 dislike
1 answer
asked Aug 17, 2019 by kingdomofcrooked
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
28,002 users