Friday, March 30, 2018

Microsoft guidance on throwing exceptions

The rest of this post is a quote from this article at https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2201-do-not-raise-reserved-exception-types:
The following exception types are too general to provide sufficient information to the user:

Throw Specific Exceptions
The following table shows parameters and which exceptions to throw when you validate the parameter, including the value parameter in the set accessor of a property:

Parameter DescriptionException
null reference System.ArgumentNullException
Outside the allowed range of values (such as an index for a collection or list) System.ArgumentOutOfRangeException
Invalid enum value System.ComponentModel.InvalidEnumArgumentException
Contains a format that does not meet the parameter specifications of a method (such as the format string for ToString(String)) System.FormatException
Otherwise invalid System.ArgumentException