ArgumentException

Still in the proximity of the bug-indicating exceptions category, we’ve landed at exception types you should be throwing yourself as well to indicate your API users are doing something silly or unexpected. ArgumentException is such an exception, signaling a parameter has an unsupported value.

For example, the File.OpenRead method will throw this exception when:

path is a zero-length string, contains only white space, or contains one or moreinvalid characters as defined by System.IO.Path.InvalidPathChars.

When accepting a path from some external source (such as the user), those requirements can be checked before calling the API. Alternatively, you could catch the exception to signal the mishap, but checks upfront are generally ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.