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.

Catching exceptions ❘ 355<br />

Now your try block looks like this:<br />

try<br />

{<br />

// code for normal execution<br />

if (Overflow == true)<br />

{<br />

throw new OverflowException();<br />

}<br />

// more processing<br />

if (OutOfBounds == true)<br />

{<br />

throw new IndexOutOfRangeException();<br />

}<br />

// otherwise continue normal execution<br />

}<br />

catch (OverflowException ex)<br />

{<br />

// error h<strong>and</strong>ling for the overflow error condition<br />

}<br />

catch (IndexOutOfRangeException ex)<br />

{<br />

// error h<strong>and</strong>ling for the index out of range error condition<br />

}<br />

finally<br />

{<br />

// clean up<br />

}<br />

So far, this might not look that much different from what you could have done a long time ago if you ever<br />

used the Visual Basic 6 On Error GoTo statement (with the exception perhaps that the different parts in the<br />

code are separated). <strong>C#</strong>, however, provides a far more powerful <strong>and</strong> flexible mechanism for error h<strong>and</strong>ling.<br />

This is because you can have throw statements that are nested in several method calls inside the try block,<br />

but the same try block continues to apply even as execution flow enters these other methods. If<br />

the computer encounters a throw statement, it immediately goes back up through all the method calls on the<br />

stack, looking for the end of the containing try block <strong>and</strong> the start of the appropriate catch block. During<br />

this process, all the local variables in the intermediate method calls will correctly go out of scope. This<br />

makes the try...catch architecture well suited to the situation described at the beginning of this section,<br />

where the error occurs inside a method call that is nested inside 15 or 20 method calls, <strong>and</strong> processing has<br />

to stop immediately.<br />

As you can probably gather from this discussion, try blocks can play a very significant part in controlling<br />

the flow of execution of your code. However, it is important to underst<strong>and</strong> that exceptions are intended for<br />

exceptional conditions, hence their name. You wouldn’t want to use them as a way of controlling when to<br />

exit a do...while loop.<br />

implementing multiple Catch blocks<br />

The easiest way to see how try...catch...finally blocks work in practice is with a couple of examples.<br />

The first example is called SimpleExceptions. It repeatedly asks the user to type in a number <strong>and</strong> then<br />

displays it. However, for the sake of this example, imagine that the number has to be between 0 <strong>and</strong><br />

5; otherwise, the program won’t be able to process the number properly. Therefore, you will throw an<br />

exception if the user types in anything outside of this range.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!