O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required