English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Exception Classes (Exception)

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

C#.NET provides built-in exception classes for each possible error. The Exception class is the base class for all exception classes.

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

.NET Exception Classes

In the figure above, the Exception class is the base class of the SystemException and ApplicationException classes. The SystemException class is the base class for all exceptions that can occur during the execution of the program. The ApplicationException class should be derived to create your own custom exception classes. Custom classes can be created for violations of business rules or other application-related errors.

The figure below shows how a NullReferenceException is thrown when accessing a null object property at runtime in the Visual Studio debugging mode.

NullReferenceException

Built-in Exception Classes

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

Exception CategoryDescription
ArgumentExceptionThrown when a non-null parameter passed to a method is invalid.
ArgumentNullExceptionThrown when a null parameter is passed to a method.
ArgumentOutOfRangeExceptionThrown when the parameter value is outside the valid range.
DivideByZeroExceptionThrown when an integer value is divided by zero.
FileNotFoundExceptionThrown when a physical file does not exist at the specified location.
FormatExceptionThrown when the format of the value is not suitable for conversion from a string using a conversion method (such as Parse).
IndexOutOfRangeExceptionThrown when the array index is outside the lower or upper limit of the array or collection.
InvalidOperationExceptionThrown when a method call is invalid in the current state of the object.
KeyNotFoundExceptionThrown when a specified key does not exist to access the members of the collection.
NotSupportedExceptionThrown when a method or operation is not supported.
NullReferenceExceptionThrown when the program accesses a member of an empty object.
OverflowExceptionThrown when an overflow occurs during arithmetic, cast, or conversion operations.
OutOfMemoryExceptionThrown when the program does not have enough memory to execute the code.
StackOverflowExceptionThrown when a stack overflow occurs in memory.
TimeoutExceptionThe time interval allocated to the operation has expired.

When an exception occurs, the application code or the default handler will handle the exception. Learn how to handle exception situations in the next section.