Exceptions in C# provide a structured, uniform, and type-safe way of handling both system level and application-level error conditions.
21.2 Causes of exceptions
Exception can be thrown in two different ways.
throwstatement (§13.10.6) throws an exception immediately and unconditionally. Control never reaches the statement immediately following the
- Certain exceptional conditions that arise during the processing of C# statements and expression cause an exception in certain circumstances when the operation cannot be completed normally. See §21.5 for a list of the various exceptions that can occur in this way.
Example: An integer division operation (§12.10.3) throws a
System.DivideByZeroExceptionif the denominator is zero. end example
21.3 The System.Exception class
System.Exception class is the base type of all exceptions. This class has a few notable properties that all exceptions share:
Messageis a read-only property of type
stringthat contains a human-readable description of the reason for the exception.
InnerExceptionis a read-only property of type
Exception. If its value is non-
null, it refers to the exception that caused the current exception. (That is, the current exception was raised in a catch block handling the
InnerException.) Otherwise, its value is
null, indicating that this exception was not caused by another exception. The number of exception objects chained together in this manner can be arbitrary.
The value of these properties can be specified in calls to the instance constructor for
21.4 How exceptions are handled
Exceptions are handled by a
try statement (§13.11).
When an exception occurs, the system searches for the nearest catch clause that can handle the exception, as determined by the run-time type of the exception. First, the current method is searched for a lexically enclosing
try statement, and the associated
catch clauses of the
try statement are considered in order. If that fails, the method that called the current method is searched for a lexically enclosing
try statement that encloses the point of the call to the current method. This search continues until a
catch clause is found that can handle the current exception, by naming an exception class that is of the same class, or a base class, of the run-time type of the exception being thrown. A
catch clause that doesn’t name an exception class can handle any exception.
Once a matching
catch clause is found, the system prepares to transfer control to the first statement of the
catch clause. Before execution of the
catch clause begins, the system first executes, in order, any
finally clauses that were associated with
try statements more nested that than the one that caught the exception.
If no matching
catch clause is found:
- If the search for a matching
catchclause reaches a static constructor (§15.12) or static field initializer, then a
System.TypeInitializationExceptionis thrown at the point that triggered the invocation of the static constructor. The inner exception of the
System.TypeInitializationExceptioncontains the exception that was originally thrown.
- Otherwise, if an exception occurs during finalizer execution, and that exception is not caught, then the behavior is unspecified.
- Otherwise, if the search for matching
catchclauses reaches the code that initially started the thread, then execution of the thread is terminated. The impact of such termination is implementation-defined.
21.5 Common exception classes
The following exceptions are thrown by certain C# operations.
||A base class for exceptions that occur during arithmetic operations, such as
||Thrown when a store into an array fails because the type of the stored element is incompatible with the type of the array.|
||Thrown when an attempt to divide an integral value by zero occurs.|
||Thrown when an attempt to index an array via an index that is less than zero or outside the bounds of the array.|
||Thrown when an explicit conversion from a base type or interface to a derived type fails at run-time.|
||Thrown when a
||Thrown when an attempt to allocate memory (via
||Thrown when an arithmetic operation in a
||Thrown when the execution stack is exhausted by having too many pending calls; typically indicative of very deep or unbounded recursion.|
||Thrown when a static constructor or static field initializer throws an exception, and no