AllNumbersZeroException:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

public class AllNumbersZeroException : Exception {

 public AllNumbersZeroException() {}

 public AllNumbersZeroException(string message) : base(message) {}

 public AllNumbersZeroException(string message, Exception inner) : base(message, inner) {}

}

To create your own custom exception class, you need to inherit from the Exception base class and implement the three overloaded constructors for it.

The AllNumbersZeroException class contains three overloaded constructors that initialize the constructor in the base class. To see how you can use this custom exception class, let's take another look at the program you have been using all along:

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

  Console.WriteLine(ex.Message);

 } 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 && num2 == 0) {

  AllNumbersZeroException ex =

   new AllNumbersZeroException('Both numbers cannot be 0.') {

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

   };

  throw ex;

 }

 if (num1 == 0) {

  ArithmeticException ex =

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

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

   };

  throw ex;

 }

 if (num2 == 0) {

  DivideByZeroException ex =

   new DivideByZeroException('Value of num2 cannot be 0.') {

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

   };

  throw ex;

 }

 return num1 / num2;

}

This program shows that if both num1 and num2 are zero, the AllNumbersException exception is raised with the custom message set.

Here's the output when 0 is entered for both num1 and num2:

Please enter the first number:0

Please enter the second number:0

Both numbers cannot be 0.

Summary

Handling exceptions is part and parcel of the process of building a robust application, and you should spend considerable effort in identifying code that is likely to cause an exception. Besides catching all the exceptions defined in the .NET Framework, you can also define your own custom exception containing your own specific error message.

Chapter 13

Arrays and Collections

In programming, you often need to work with collections of related data. For example, you may have a list of customers and you need a way to store their email addresses. In that case, you can use an array to store the list of strings.

In .NET, there are many collection classes that you can use to represent groups of data. In addition, there are various interfaces that you can implement so that you can manipulate your own custom collection of data.

This chapter examines:

□ Declaring and initializing arrays

□ Declaring and using multidimensional arrays

□ Declaring a parameter array to allow a variable number of parameters in a function

□ Using the various System.Collections namespace interfaces

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

0

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

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