logo search
Учебник_ПОА

Перехват исключения

В этом примере используются блоки try-catch для выявления исключительной ситуации деления на ноль. После перехвата исключения выполнение продолжается в блоке finally.

Пример

int top = 0, bottom = 0, result = 0;

try

{

result = top / bottom;

}

catch (System.Exception ex)

{

System.Console.WriteLine("{0} exception caught here.", ex.GetType().ToString());

System.Console.WriteLine(ex.Message);

}

finally

{

System.Console.WriteLine("Clean-up code executes here...");

}

System.Console.WriteLine("Program execution continues here...");

// Результат

System.DivideByZeroException exception caught here.

Attempted to divide by zero.

Clean-up code executes here...

Program execution continues here...