Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

C H A P T E R 11

Exceptions

What Are Exceptions?

The try Statement

The Exception Classes

The catch Clause

Examples Using Specific catch Clauses

The catch Clauses Section

The finally Block

Finding a Handler for an Exception

Searching Further

Throwing Exceptions

Throwing Without an Exception Object

297

CHAPTER 11 EXCEPTIONS

What Are Exceptions?

An exception is a run-time error in a program that violates a system or application constraint, or a condition that is not expected to occur during normal operation. Examples are when a program tries to divide a number by zero or tries to write to a read-only file. When these occur, the system catches the error and raises an exception.

If the program has not provided code to handle the exception, the system will halt the program. For example, the following code raises an exception when it attempts to divide by zero:

static void Main()

{

int x =

10, y = 0;

x /= y;

// Attempt to divide by zero--raises an exception

}

When this code is run, the system displays the following error message:

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at Exceptions_1.Program.Main() in C:\Progs\Exceptions\Program.cs:line 12

298

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]