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 following figure shows how a NullReferenceException is triggered when accessing a null object property in the Visual Studio debugging mode.
The following table lists important built-in exception classes in .NET.
Exception Category | Description |
---|---|
ArgumentException | Triggered when a non-null parameter passed to a method is invalid. |
ArgumentNullException | Triggered when a null parameter is passed to a method. |
ArgumentOutOfRangeException | Triggered when the parameter value is outside the valid range. |
DivideByZeroException | Triggered when an integer value is divided by zero. |
FileNotFoundException | Triggered when a physical file does not exist at the specified location. |
FormatException | Triggered when the format of the value is not suitable for conversion from a string using a conversion method (such as Parse). |
IndexOutOfRangeException | Triggered when the array index is outside the lower or upper limit of the array or collection. |
InvalidOperationException | Triggered when a method call is invalid in the current state of the object. |
KeyNotFoundException | Triggered when the specified key does not exist to access the members of the collection. |
NotSupportedException | Triggered when a method or operation is not supported. |
NullReferenceException | Gera exceção quando o programa acessa um membro de objeto nulo. |
OverflowException | Gera exceção quando uma operação aritmética, conversão forçada ou conversão resulta em overflow. |
OutOfMemoryException | Gera exceção quando o programa não tem memória suficiente para executar o código. |
StackOverflowException | Gera exceção quando o pilha na memória se esgota. |
TimeoutException | O intervalo de tempo atribuído à operação expirou. |
Quando ocorre um erro, o código do aplicativo ou o manipulador padrão tratam as exceções. No próximo capítulo, aprenda como lidar com situações de exceção.