Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

} catch (DivideByZeroException ex) {

Console.WriteLine("Division by zero error.");

} catch (ArithmeticException ex) {

Console.WriteLine("Numerator cannot be zero.");

} catch (FormatException ex) {

Console.WriteLine("Input error.");

} catch (Exception ex) {

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

The following shows the output when different values are entered for num1and num2:

Please enter the first number:5

Please enter the second number:0

Division by zero error.

Please enter the first number:0

Please enter the second number:5

Numerator cannot be zero.

Please enter the first number:a

Input error.

Rethrowing Exceptions

There are times when after catching an exception, you want to throw the same (or a new type) exception back to the calling function after taking some corrective actions. Take a look at this example:

class Program {

static void Main(string[] args) {

int num1, num2, result;

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 (Exception ex) {

Console.WriteLine(ex.Message);

if (ex.InnerException != null)

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

}

Console.ReadLine();

}

private int PerformDivision(int num1, int num2) {

try {

return num1 / num2;

} catch (DivideByZeroException ex) {

throw new Exception("Division by zero error.", ex);

}

}

}

Here, the PerformDivision()function tries to catch the DivideByZeroExceptionexception and once it succeeds, it rethrows a new generic Exceptionexception, using the following statements with two arguments:

throw new Exception("Division by zero error.", ex);

The first argument indicates the description for the exception to be thrown, while the second argument is for the inner exception. The inner exception indicates the exception that causes the current exception. When this exception is rethrown, it is handled by the catch block in the Main()function:

catch (Exception ex) {

Console.WriteLine(ex.Message);

if (ex.InnerException != null)

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

}

To retrieve the source of the exception, you can check the InnerExceptionproperty and print out its details using the ToString()method. Here's the output when num2 is zero:

Please enter the first number:5

Please enter the second number:0

Division by zero error.

System.DivideByZeroException: Attempted to divide by zero.

at ConsoleApp.Program.PerformDivision(Int32 num1, Int32 num2) in C:\Documents and Settings\Wei-Meng Lee\My Documents\Visual Studio 2008\Projects\ConsoleApp\ConsoleApp\Program.cs:line 43

As you can see, the message of the exception is "Division by zero error" (set by yourself) and the InnerExceptionproperty shows the real cause of the error — "Attempted to divide by zero."

Exception Chaining

The InnerExceptionproperty is of type Exception, and it can be used to store a list of previous exceptions. This is known as exception chaining.

To see how exception chaining works, consider the following program:

class Program {

static void Main(string[] args) {

Program myApp = new Program();

try {

myApp.Method1();

} catch (Exception ex) {

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 Exceptionexception by passing in the current exception ( DividebyZeroException). This exception is caught by Method2(), which rethrows a new Exceptionexception by passing in the current exception. Method1()in turn catches the exception and rethrows a new Exceptionexception. Finally, the Main()function catches the exception and prints out the result as shown in Figure 12-4.

Figure 124 If you set a breakpoint in the catch block within the - фото 184

Figure 12-4

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

Figure 125 Using Exception Objects Instead of using the default description - фото 185

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 Messageproperty. You can also specify the HelpLinkproperty to point to a URL where developers can find more information about the exception. For example, you can create a new instance of the ArithmeticExceptionclass using the following code:

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x