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 ❘ 361<br />

{<br />

// error h<strong>and</strong>ling<br />

}<br />

finally<br />

{<br />

// clean up<br />

}<br />

Although each try block is accompanied by only one catch block in this example, you could string several<br />

catch blocks together, too. This section takes a closer look at how nested try blocks work.<br />

If an exception is thrown inside the outer try block but outside the inner try block (points A <strong>and</strong> D), the<br />

situation is no different from any of the scenarios you have seen before: either the exception is caught by the<br />

outer catch block <strong>and</strong> the outer finally block is executed, or the finally block is executed <strong>and</strong> the .<strong>NET</strong><br />

runtime h<strong>and</strong>les the exception.<br />

If an exception is thrown in the inner try block (point B), <strong>and</strong> there is a suitable inner catch block to<br />

h<strong>and</strong>le the exception, then, again, you are in familiar territory: the exception is h<strong>and</strong>led there, <strong>and</strong> the inner<br />

finally block is executed before execution resumes inside the outer try block (at point D).<br />

Now suppose that an exception occurs in the inner try block, but there isn ’ t a suitable inner catch block to<br />

h<strong>and</strong>le it. This time, the inner finally block is executed as usual, but then the .<strong>NET</strong> runtime will have no<br />

choice but to leave the entire inner try block to search for a suitable exception h<strong>and</strong>ler. The next obvious<br />

place to look is in the outer catch block. If the system fi nds one here, then that h<strong>and</strong>ler will be executed <strong>and</strong><br />

then the outer finally block will be executed. If there is no suitable h<strong>and</strong>ler here, the search for one will<br />

go on. In this case, it means the outer finally block will be executed, <strong>and</strong> then, because there are no more<br />

catch blocks, control will be transferred to the .<strong>NET</strong> runtime. Note that the code beyond point D in the<br />

outer try block is not executed at any point.<br />

An even more interesting thing happens if an exception is thrown at point C. If the program is at point C, it<br />

must be already processing an exception that was thrown at point B. It is quite legitimate to throw another<br />

exception from inside a catch block. In this case, the exception is treated as if it had been thrown by the<br />

outer try block, so fl ow of execution will immediately leave the inner catch block, <strong>and</strong> execute the inner<br />

finally block, before the system searches the outer catch block for a h<strong>and</strong>ler. Similarly, if an exception is<br />

thrown in the inner finally block, control will immediately be transferred to the best appropriate h<strong>and</strong>ler,<br />

with the search starting at the outer catch block.<br />

It is perfectly legitimate to throw exceptions from catch <strong>and</strong> finally blocks.<br />

Although the situation has been shown with just two try blocks, the same principles hold no matter<br />

how many try blocks you nest inside each other. At each stage, the .<strong>NET</strong> runtime will smoothly<br />

transfer control up through the try blocks, looking for an appropriate h<strong>and</strong>ler. At each stage, as<br />

control leaves a catch block, any cleanup code in the corresponding finally block (if present) will be<br />

executed, but no code outside any finally block will be run until the correct catch h<strong>and</strong>ler has been<br />

found <strong>and</strong> run.<br />

The nesting of try blocks can also occur between methods themselves. For example, if method A calls<br />

method B from within a try block, then method B itself has a try block within it as well.<br />

You have now seen how having nested try blocks can work. The obvious next question is why would you<br />

want to do that There are two reasons:<br />

➤<br />

➤<br />

To modify the type of exception thrown<br />

To enable different types of exception to be h<strong>and</strong>led in different places in your code<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!