15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

362 ❘ ChaPTer 15 errOrs And exceptiOns<br />

Modifying the Type of exception<br />

Modifying the type of the exception can be useful when the original exception thrown does not adequately<br />

describe the problem. What typically happens is that something — possibly the .<strong>NET</strong> runtime — throws<br />

a fairly low-level exception that says something like an overflow occurred (OverflowException) or an<br />

argument passed to a method was incorrect (a class derived from ArgumentException). However, because<br />

of the context in which the exception occurred, you will know that this reveals some other underlying<br />

problem (for example, an overflow can only happen at that point in your code because a file you have<br />

just read contained incorrect data). In that case, the most appropriate thing that your h<strong>and</strong>ler for the<br />

first exception can do is throw another exception that more accurately describes the problem, so that<br />

another catch block further along can deal with it more appropriately. In this case, it can also forward<br />

the original exception through a property implemented by System.Exception called InnerException.<br />

InnerException simply contains a reference to any other related exception that was thrown — in case the<br />

ultimate h<strong>and</strong>ler routine will need this extra information.<br />

Of course, the situation also exists where an exception occurs inside a catch block. For example, you might<br />

normally read in some configuration file that contains detailed instructions for h<strong>and</strong>ling the error, <strong>and</strong> it<br />

might turn out that this file is not there.<br />

H<strong>and</strong>ling Different exceptions in Different Places<br />

The second reason for having nested try blocks is so that different types of exceptions can be h<strong>and</strong>led<br />

at different locations in your code. A good example of this is if you have a loop where various exception<br />

conditions can occur. Some of these might be serious enough that you need to ab<strong>and</strong>on the entire loop,<br />

whereas others might be less serious <strong>and</strong> simply require that you ab<strong>and</strong>on that iteration <strong>and</strong> move on to<br />

the next iteration around the loop. You could achieve this by having one try block inside the loop, which<br />

h<strong>and</strong>les the less serious error conditions, <strong>and</strong> an outer try block outside the loop, which h<strong>and</strong>les the more<br />

serious error conditions. You will see how this works in the next exceptions example.<br />

user-defined eXCePTion Classes<br />

You are now ready to look at a second example that illustrates exceptions. This example, called<br />

SolicitColdCall, contains two nested try blocks <strong>and</strong> illustrates the practice of defining your own custom<br />

exception classes <strong>and</strong> throwing another exception from inside a try block.<br />

This example assumes that a sales company wants to have additional customers on its sales list. The<br />

company’s sales team is going to phone a list of people to invite them to become customers, a practice<br />

known in sales jargon as cold calling. To this end, you have a text file available that contains the names of<br />

the people to be cold called. The file should be in a well-defined format in which the first line contains the<br />

number of people in the file <strong>and</strong> each subsequent line contains the name of the next person. In other words,<br />

a correctly formatted file of names might look like this:<br />

4<br />

George Washington<br />

Benedict Arnold<br />

John Adams<br />

Thomas Jefferson<br />

This version of cold calling is designed to display the name of the person on the screen (perhaps for the<br />

salesperson to read). That is why only names <strong>and</strong> not phone numbers of the individuals are contained in the file.<br />

For this example, your program will ask the user for the name of the file <strong>and</strong> will then simply read it in <strong>and</strong><br />

display the names of people. That sounds like a simple task, but even so, a couple of things can go wrong<br />

<strong>and</strong> require you to ab<strong>and</strong>on the entire procedure:<br />

➤<br />

The user might type the name of a file that does not exist. This will be caught as a FileNotFound<br />

exception.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!