Tutorialsteacher

Follow Us

Built-in Exception Classes in C#

Here you will learn about the built-in exception classes in C#.

C# .NET includes built-in exception classes for every possible error. The Exception class is the base class of all the exception classes.

The following is a hierarchy of exception classes in .NET:

Exception Classes in .NET

In the above figure, the Exception class is the base class of the SystemException and ApplicationException classes. The SystemException class is the base class for all the built-in exception classes in .NET Framework.

The ApplicationException was recommended to be base class for all your custom exceptions classes (The custom exeception class should be created if non of the system exception classes can be used and you need new exception class for business rule violations or for other application related errors). It was meant to differentiates between exceptions defined by applications versus exceptions defined by the system. However, Microsoft now recommends to derive custom exception classes from the Exception class rather than the ApplicationException class .

The following figure shows how the NullReferenceException is thrown in Visual Studio debug mode when you access a null object property at runtime.

NullReferenceException

Built-in Exception Classes

The following table lists important built-in exception classes in .NET.

Exception Class Description
ArgumentExceptionRaised when a non-null argument that is passed to a method is invalid.
ArgumentNullExceptionRaised when null argument is passed to a method.
ArgumentOutOfRangeExceptionRaised when the value of an argument is outside the range of valid values.
DivideByZeroExceptionRaised when an integer value is divide by zero.
FileNotFoundExceptionRaised when a physical file does not exist at the specified location.
FormatExceptionRaised when a value is not in an appropriate format to be converted from a string by a conversion method such as Parse.
IndexOutOfRangeExceptionRaised when an array index is outside the lower or upper bounds of an array or collection.
InvalidOperationExceptionRaised when a method call is invalid in an object's current state.
KeyNotFoundExceptionRaised when the specified key for accessing a member in a collection is not exists.
NotSupportedExceptionRaised when a method or operation is not supported.
NullReferenceExceptionRaised when program access members of null object.
OverflowExceptionRaised when an arithmetic, casting, or conversion operation results in an overflow.
OutOfMemoryExceptionRaised when a program does not get enough memory to execute the code.
StackOverflowExceptionRaised when a stack in memory overflows.
TimeoutExceptionThe time interval allotted to an operation has expired.

When an error occurs, either the application code or the default handler handles the exception. Learn how to handle the excetion in the next section.