Console.WriteLine(ex.Message);

   if (ex.InnerException != null)

    Console.WriteLine(ex.InnerException.ToString());

  }

  Console.ReadLine();

 }

 private void Method1() {

  try {

   Method2();

  } catch (Exception ex) {

   throw new Exception(

    'Exception caused by calling Method2() in Method1().', ex);

  }

 }

 private void Method2() {

  try {

   Method3();

  } catch (Exception ex) {

   throw new Exception(

    'Exception caused by calling Method3() in Method2().', ex);

  }

 }

 private void Method3() {

  try {

   int num1 = 5, num2 = 0;

   int result = num1 / num2;

  } catch (DivideByZeroException ex) {

   throw new Exception('Division by zero error in Method3().', ex);

  }

 }

}

In this program, the Main() function calls Method1(), which in turns calls Method2(). Method2() then calls Method3(). In Method3 (), a division-by-zero exception occurs and you rethrow a new Exception exception by passing in the current exception (DividebyZeroException). This exception is caught by Method2(), which rethrows a new Exception exception by passing in the current exception. Method1() in turn catches the exception and rethrows a new Exception exception. Finally, the Main() function catches the exception and prints out the result as shown in Figure 12-4.

Figure 12-4

If you set a breakpoint in the catch block within the Main() function, you will see that the InnerException property contains details of each exception and that all the exceptions are chained via the InnerException property (see Figure 12-5).

Figure 12-5 

Using Exception Objects

Instead of using the default description for each exception class you are throwing, you can customize the description of the exception by creating an instance of the exception and then setting the Message property. You can also specify the HelpLink property to point to a URL where developers can find more information about the exception. For example, you can create a new instance of the ArithmeticException class using the following code:

if (num1 == 0) {

 ArithmeticException ex =

  new ArithmeticException('Value of num1 cannot be 0.') {

   HelpLink = 'http://www.learn2develop.net'

  };

 throw ex;

}

Here's how you can modify the previous program by customizing the various existing exception classes:

class Program {

 static void Main(string[] args) {

  int num1, num2;

  try {

   Console.Write('Please enter the first number:');

   num1 = int.Parse(Console.ReadLine());

   Console.Write('Please enter the second number:');

   num2 = int.Parse(Console.ReadLine());

   Program myApp = new Program();

   Console.WriteLine('The result of {0}/{1} is {2}', num1, num2,

    myApp.PerformDivision(num1, num2));

  } catch (DivideByZeroException ex) {

   Console.WriteLine(ex.Message);

  } catch (ArithmeticException ex) {

   Console.WriteLine(ex.Message);

  } catch (FormatException ex) {

   Console.WriteLine(ex.Message);

  } catch (Exception ex) {

   Console.WriteLine(ex.Message);

  }

  Console.ReadLine();

 }

 private int PerformDivision(int num1, int num2) {

  if (num1 == 0) {

   ArithmeticException ex =

    new ArithmeticException('Value of num1 cannot be 0.') {

     HelpLink = 'http://www.learn2develop.net'

    };

   throw ex;

  }

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату