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.

400 ❘ ChaPTer 16 visuAl studiO 2010<br />

figure 16-26<br />

exceptions<br />

Exceptions are great when you ship your application <strong>and</strong> for making sure that error conditions are h<strong>and</strong>led<br />

in an appropriate way within your application. Used well, they can ensure that your application copes with<br />

difficulties well <strong>and</strong> that the user is never presented with a technical dialog box. Unfortunately, exceptions<br />

are not so great when you are trying to debug your application. The problem is twofold:<br />

➤<br />

➤<br />

If an exception occurs when you are debugging, you often do not want it to be h<strong>and</strong>led<br />

automatically — especially if automatically h<strong>and</strong>ling it means retiring gracefully <strong>and</strong> terminating<br />

execution! Rather, you want the debugger to help you find why the exception has occurred. Of course,<br />

the trouble is that if you have written good, robust, defensive code, your program will automatically<br />

h<strong>and</strong>le almost anything — including the bugs that you want to detect!<br />

If an exception occurs that you have not written a h<strong>and</strong>ler for, the .<strong>NET</strong> runtime will still go off looking<br />

for a h<strong>and</strong>ler. However, by the time it discovers that there is not one, it will have terminated your<br />

program. There will not be a call stack left, <strong>and</strong> you will not be able to look at the values of any of<br />

your variables because they will all have gone out of scope.<br />

Of course, you can set breakpoints in your catch blocks, but that often does not help very much because<br />

when the catch block is reached, flow of execution will, by definition, have exited the corresponding try<br />

block. That means that the variables you probably wanted to examine the values of to figure out what has<br />

gone wrong will have gone out of scope. You will not even be able to look at the stack trace to find<br />

what method was being executed when the throw statement occurred — because control will have left<br />

that method. Setting the breakpoints at the throw statement will of course solve this, except that if you<br />

are coding defensively, there will be many throw statements in your code. How can you tell which one is the<br />

one that threw the exception<br />

In fact, Visual Studio provides a very neat answer to all of this. If you look into the main Debug menu, you<br />

will find a menu item called Exceptions. This item opens the Exceptions dialog box (see Figure 16-27), which<br />

allows you to specify what happens when an exception is thrown. You can choose to continue execution<br />

or to stop <strong>and</strong> start debugging — in which case execution stops <strong>and</strong> the debugger steps in at the throw<br />

statement itself.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!