O'Reilly logo

C# 5.0 Programmer's Reference by Rod Stephens

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

Appendix O Useful Exception Classes

When your program throws an exception, you can use a try-catch-finally block to catch the exception and examine it to determine its class. When you want to throw your own exception, however, you must know what exception classes are available so that you can pick the best one to throw. The following sections describe some of the most useful classes for throwing exceptions.

Standard Exception Classes

The following table lists some of the most useful exception classes in C#. If possible you should use one of these standard classes when you need to throw an exception.

ClassPurpose
AmbiguousMatchExceptionThe program could not figure out which overloaded object method to use.
ArgumentExceptionAn argument is invalid.
ArgumentNullExceptionAn argument that cannot be null has the value null.
ArgumentOutOfRangeExceptionAn argument is out of its allowed range.
ArithmeticExceptionAn arithmetic, casting, or conversion operation has occurred.
ArrayTypeMismatchExceptionThe program tried to store the wrong type of item in an array.
ConfigurationExceptionA configuration setting is invalid.
ConstraintExceptionA data operation violates a database constraint.
DataExceptionThe ancestor class for ADO.NET exception classes.
DirectoryNotFoundExceptionA needed directory is missing.
DivideByZeroExceptionThe program tried to divide by zero.
DuplicateNameExceptionAn ADO.NET operation encountered a duplicate name. For example, it tried to create two tables ...

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