Custom Exceptions


Earlier in the book, in Chapter 7, you looked at exceptions and how you can use try . . . catch . . . finally blocks to act on them. You also saw several standard .NET exceptions, including the base class for exceptions System.Exception. Sometimes, it can be useful to derive your own exception classes from this base class and use them in your applications, instead of using the standard exceptions. This allows you to be more specific about the information you send to whatever code catches the exception and allows catching code to be more specific about which exceptions it handles. You might, for example, add a new property to your exception class that permits access to some underlying information, making it possible for the receiver of the exception to make the required changes, or just giving more information as to the exception cause.

Once you have defined an exception class you can add it to the list of exceptions recognized by VS using the Debug Exceptions... dialog's Add button, then defining exception-specific behavior as you saw in Chapter 7.

Adding Custom Exceptions to CardLib

The use of custom exceptions is, once again, best illustrated by upgrading the CardLib project. the Deck.GetCard() method currently throws a standard .NET exception if an attempt is made to access a card with an index less than 0 or greater than 51, but you'll modify this to use a custom exception.

First, you need to create a new class library project called Ch13CardLib and copy the classes from Ch12CardLib as before, changing the namespace to Ch13CardLib as applicable. Next, you define the exception. You do this with a new class defined in a new class file called CardOutOfRangeException.cs, which you can add to the Ch13CardLib project with Project Add New Item:

 public class CardOutOfRangeException : Exception { private Cards deckContents; public Cards DeckContents { get { return deckContents; } } public CardOutOfRangeException(Cards sourceDeckContents) : base("There are only 52 cards in the deck.") { deckContents = sourceDeckContents; } } 

An instance of the Cards class is required for the constructor of this class. It allows access to this Cards object through a DeckContents property and supplies a suitable error message to the base Exception constructor, so that it is available through the Message property of the class.

Next, you add code to throw this exception to Deck.cs (replacing the old standard exception):

public Card GetCard(int cardNum) {    if (cardNum >= 0 && cardNum <= 51)       return cards[cardNum];    else throw new CardOutOfRangeException(cards.Clone() as Cards); }

The DeckContents property is initialized with a deep copy of the current contents of the Deck object, in the form of a Cards object. This means that you see what the contents were at the point where the exception was thrown, so subsequent modification to the deck contents won't "lose" this information.

To test this, you can use the following client code (in Ch13CardClient in the downloadable code for this chapter):

 Deck deck1 = new Deck(); try { Card myCard = deck1.GetCard(60); } catch (CardOutOfRangeException e) { Console.WriteLine(e.Message); Console.WriteLine(e.DeckContents[0]); } 

This code gives the output shown in Figure 13-1.

image from book
Figure 13-1

Here, the catching code has written the exception Message property to the screen. You also displayed the first card in the Cards object obtained through DeckContents, just to prove that you can access the Cards collection through your custom exception object.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net