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

Classe de Exceção (Exception) do C#

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 following figure shows how a NullReferenceException is triggered when accessing a null object property in the Visual Studio debugging mode.

NullReferenceException

Built-in Exception Classes

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

Exception CategoryDescription
ArgumentExceptionTriggered when a non-null parameter passed to a method is invalid.
ArgumentNullExceptionTriggered when a null parameter is passed to a method.
ArgumentOutOfRangeExceptionTriggered when the parameter value is outside the valid range.
DivideByZeroExceptionTriggered when an integer value is divided by zero.
FileNotFoundExceptionTriggered when a physical file does not exist at the specified location.
FormatExceptionTriggered when the format of the value is not suitable for conversion from a string using a conversion method (such as Parse).
IndexOutOfRangeExceptionTriggered when the array index is outside the lower or upper limit of the array or collection.
InvalidOperationExceptionTriggered when a method call is invalid in the current state of the object.
KeyNotFoundExceptionTriggered when the specified key does not exist to access the members of the collection.
NotSupportedExceptionTriggered when a method or operation is not supported.
NullReferenceExceptionGera exceção quando o programa acessa um membro de objeto nulo.
OverflowExceptionGera exceção quando uma operação aritmética, conversão forçada ou conversão resulta em overflow.
OutOfMemoryExceptionGera exceção quando o programa não tem memória suficiente para executar o código.
StackOverflowExceptionGera exceção quando o pilha na memória se esgota.
TimeoutExceptionO 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.