Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 23 Exceptions

123. Exceptions

2Exceptions in C# provide a structured, uniform, and type-safe way of handling both system level and

3application-level error conditions. [Note: The exception mechanism in C# is quite similar to that of C++,

4with a few important differences:

5In C#, all exceptions shall be represented by an instance of a class type derived from

6System.Exception. In C++, any value of any type can be used to represent an exception.

7In C#, a finally block (§15.10) can be used to write termination code that executes in both normal

8execution and exceptional conditions. C++ has no equivalent construct.

9In C#, system-level exceptions such as overflow, divide-by-zero, and null dereferences have well

10defined exception classes and are on a par with application-level error conditions.

11end note]

1223.1 Causes of exceptions

13Exception can be thrown in two different ways.

14A throw statement (§15.9.5) throws an exception immediately and unconditionally. Control never

15reaches the statement immediately following the throw.

16Certain exceptional conditions that arise during the processing of C# statements and expression cause an

17exception in certain circumstances when the operation cannot be completed normally. [Example: An

18integer division operation (§14.7.2) throws a System.DivideByZeroException if the denominator is

19zero. end example] See §23.4 for a list of the various exceptions that can occur in this way.

2023.2 The System.Exception class

21The System.Exception class is the base type of all exceptions. This class has a few notable properties

22that all exceptions share:

23Message is a read-only property of type string that contains a human-readable description of the

24reason for the exception.

25InnerException is a read-only property of type Exception. If its value is non-null, it refers to the

26exception that caused the current exception. (That is, the current exception was raised in a catch block

27handling the type InnerException.) Otherwise, its value is null, indicating that this exception was not

28caused by another exception. (The number of exception objects chained together in this manner can be

29arbitrary.)

30The value of these properties can be specified in calls to the instance constructor for System.Exception.

31[Note: The Standard Library provides two types that inherit directly from System.Exception:

32System.SystemException and System.ApplicationException. These classes are provided as a

33means to differentiate between exceptions defined by the system versus exceptions defined by applications,

34respectively. As such, it is recommended that exception classes be derived from one of these classes as

35appropriate, rather than from System.Exception directly. end note]

3623.3 How exceptions are handled

37Exceptions are handled by a try statement (§15.10).

38When an exception occurs, the system searches for the nearest catch clause that can handle the exception,

39as determined by the run-time type of the exception. First, the current method is searched for a lexically

361

C# LANGUAGE SPECIFICATION

1enclosing try statement, and the associated catch clauses of the try statement are considered in order. If

2that fails, the method that called the current method is searched for a lexically enclosing try statement that

3encloses the point of the call to the current method. This search continues until a catch clause is found that

4can handle the current exception, by naming an exception class that is of the same class, or a base class, of

5the run-time type of the exception being thrown. A catch clause that doesn’t name an exception class can

6handle any exception.

7Once a matching catch clause is found, the system prepares to transfer control to the first statement of the

8catch clause. Before execution of the catch clause begins, the system first executes, in order, any

9finally clauses that were associated with try statements more nested that than the one that caught the

10exception.

11If no matching catch clause is found, one of two things occurs:

12If the search for a matching catch clause reaches a static constructor (§17.11) or static field initializer,

13then a System.TypeInitializationException is thrown at the point that triggered the invocation

14of the static constructor. The inner exception of the System.TypeInitializationException

15contains the exception that was originally thrown.

16If the search for matching catch clauses reaches the code that initially started the thread, then execution

17of the thread is terminated. The impact of such termination is implementation-defined.

18Exceptions that occur during destructor execution are worth special mention. If an exception occurs during

19destructor execution, and that exception is not caught, then the execution of that destructor is terminated and

20the destructor of the base class (if any) is called. If there is no base class (as in the case of the object type)

21or if there is no base class destructor, then the exception is discarded.

2223.4 Common Exception Classes

23The following exceptions are thrown by certain C# operations.

24

362

Chapter 23 Exceptions

System.ArithmeticException

A base class for exceptions that occur during

 

arithmetic operations, such as

 

System.DivideByZeroException and

 

System.OverflowException.

System.ArrayTypeMismatchException

Thrown when a store into an array fails because the

 

actual type of the stored element is incompatible

 

with the actual type of the array.

 

 

System.DivideByZeroException

Thrown when an attempt to divide an integral

 

value by zero occurs.

 

 

System.IndexOutOfRangeException

Thrown when an attempt to index an array via an

 

index that is less than zero or outside the bounds of

 

the array.

 

 

System.InvalidCastException

Thrown when an explicit conversion from a base

 

type or interface to a derived type fails at run time.

 

 

System.NullReferenceException

Thrown when a null reference is used in a way

 

that causes the referenced object to be required.

 

 

System.OutOfMemoryException

Thrown when an attempt to allocate memory (via

 

new) fails.

 

 

System.OverflowException

Thrown when an arithmetic operation in a

 

checked context overflows.

 

 

System.StackOverflowException

Thrown when the execution stack is exhausted by

 

having too many pending method calls; typically

 

indicative of very deep or unbounded recursion.

 

 

System.TypeInitializationException

Thrown when a static constructor throws an

 

exception, and no catch clause exists to catch it.

 

 

1

363

Соседние файлы в предмете Электротехника