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 Class Hierarchy
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 exceptions that can occur during the execution of the program. The ApplicationException class should be derive to create your own custom exception class. The custom exeception class can be created for business rule violations or for other application related errors.

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

Exception
NullReferenceException

Built-in Exception Classes

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

Exception Class Description
ArgumentException Raised when a non-null argument that is passed to a method is invalid.
ArgumentNullException Raised when null argument is passed to a method.
ArgumentOutOfRangeException Raised when the value of an argument is outside the range of valid values.
DivideByZeroException Raised when an integer value is divide by zero.
FileNotFoundException Raised when a physical file does not exist at the specified location.
FormatException Raised when a value is not in an appropriate format to be converted from a string by a conversion method such as Parse.
IndexOutOfRangeException Raised when an array index is outside the lower or upper bounds of an array or collection.
InvalidOperationException Raised when a method call is invalid in an object's current state.
KeyNotFoundException Raised when the specified key for accessing a member in a collection is not exists.
NotSupportedException Raised when a method or operation is not supported.
NullReferenceException Raised when program access members of null object.
OverflowException Raised when an arithmetic, casting, or conversion operation results in an overflow.
OutOfMemoryException Raised when a program does not get enough memory to execute the code.
StackOverflowException Raised when a stack in memory overflows.
TimeoutException The 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.