English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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:
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.
The following table lists important built-in exception classes in .NET.
Exception Category | Description |
---|---|
ArgumentException | Thrown when a non-null parameter passed to a method is invalid. |
ArgumentNullException | Thrown when a null parameter is passed to a method. |
ArgumentOutOfRangeException | Thrown when the parameter value is outside the valid range. |
DivideByZeroException | Thrown when an integer value is divided by zero. |
FileNotFoundException | Thrown when a physical file does not exist at the specified location. |
FormatException | Thrown when the format of the value is not suitable for conversion from a string using a conversion method (such as Parse). |
IndexOutOfRangeException | Thrown when the array index is outside the lower or upper limit of the array or collection. |
InvalidOperationException | Thrown when a method call is invalid in the current state of the object. |
KeyNotFoundException | Thrown when a specified key does not exist to access the members of the collection. |
NotSupportedException | Thrown when a method or operation is not supported. |
NullReferenceException | Thrown when the program accesses a member of an empty object. |
OverflowException | Thrown when an overflow occurs during arithmetic, cast, or conversion operations. |
OutOfMemoryException | Thrown when the program does not have enough memory to execute the code. |
StackOverflowException | Thrown when a stack overflow occurs in memory. |
TimeoutException | The 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.