15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

366 ❘ ChaPTer 15 errOrs And exceptiOns<br />

The ProcessNextPerson() method also contains an inner try block:<br />

public void ProcessNextPerson()<br />

{<br />

if (isDisposed)<br />

{<br />

throw new ObjectDisposedException("peopleToRing");<br />

}<br />

}<br />

if (!isOpen)<br />

{<br />

throw new UnexpectedException(<br />

"Attempted to access coldcall file that is not open");<br />

}<br />

try<br />

{<br />

string name;<br />

name = sr.ReadLine();<br />

if (name == null)<br />

throw new ColdCallFileFormatException("Not enough names");<br />

if (name[0] == 'B')<br />

{<br />

throw new SalesSpyFoundException(name);<br />

}<br />

Console.WriteLine(name);<br />

}<br />

catch(SalesSpyFoundException ex)<br />

{<br />

Console.WriteLine(ex.Message);<br />

}<br />

finally<br />

{<br />

}<br />

Two possible problems exist with the file here (assuming that there actually is an open file connection;<br />

the ProcessNextPerson() method checks this first). First, you might read in the next name <strong>and</strong> discover<br />

that it is a sales spy. If that condition occurs, the exception is trapped by the first of the catch blocks in<br />

this method. Because that exception has been caught here, inside the loop, it means that execution can<br />

subsequently continue in the Main() method of the program <strong>and</strong> the subsequent names in the file will<br />

continue to be processed.<br />

A problem might also occur if you try to read the next name <strong>and</strong> discover that you have already reached<br />

the end of the file. The way that the StreamReader object’s ReadLine() method works is if it has gone<br />

past the end of the file, it doesn’t throw an exception, but simply returns null. Therefore, if you find a<br />

null string, you know that the format of the file was incorrect because the number in the first line of the<br />

file indicated a larger number of names than were actually present in the file. If that happens, you throw a<br />

ColdCallFileFormatException, which will be caught by the outer exception h<strong>and</strong>ler (which will cause the<br />

execution to terminate).<br />

Once again, you don’t need a finally block here because there is no cleanup to do; however, this time an<br />

empty finally block is included, just to show that you can do so, if you want.<br />

The example is nearly finished. You have just two more members of ColdCallFileReader to look at: the<br />

NPeopleToRing property, which returns the number of people supposed to be in the file, <strong>and</strong> the Dispose()<br />

method, which closes an open file. Notice that the Dispose() method returns only if it has already been<br />

called — this is the recommended way of implementing it. It also checks that there actually is a file stream to<br />

close before closing it. This example is shown here to illustrate defensive coding techniques, so that’s what<br />

you are doing!<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!