03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

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

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

682 x CHAPTER 18 EXCEPTION HANDLING, DEBUGGING, AND TRACING<br />

myMessage.From = New MailAddress(toAddress)<br />

myMessage.To.Add(New MailAddress(fromAddress))<br />

C#<br />

str<strong>in</strong>g fromAddress = "you@example.com";<br />

str<strong>in</strong>g toAddress = EmailAddress.Text;<br />

myMessage.From = new MailAddress(toAddress);<br />

myMessage.To.Add(new MailAddress(fromAddress));<br />

Although it’s easy to see what the problem is <strong>in</strong> this example (the To <strong>and</strong> From addresses are mixed<br />

up), it may be harder to f<strong>in</strong>d <strong>in</strong> a file with 250 l<strong>in</strong>es of code. Additionally, because the compiler happily<br />

accepts your mistake, you won’t notice the error until you see a message <strong>in</strong> your Inbox that you<br />

thought you sent to your visitors.<br />

The best way to track down <strong>and</strong> solve logic errors is us<strong>in</strong>g the built-<strong>in</strong> debugg<strong>in</strong>g capabilities of VS.<br />

You get an <strong>in</strong>-depth look at these tools later <strong>in</strong> the chapter.<br />

Runtime Errors<br />

Runtime errors occur at run time, which makes them <strong>in</strong>credibly difficult to track. Imag<strong>in</strong>e you have<br />

a site that conta<strong>in</strong>s a bug that’s hidden somewhere <strong>in</strong> a page. You haven’t found it yet, but one of<br />

your visitors did <strong>and</strong> she gets a nasty error message (more on those later) <strong>in</strong>stead. What can you<br />

do? Probably not much, because there’s a fair chance your visitor won’t even <strong>in</strong>form you about the<br />

mistake.<br />

So, it’s important to have a good error h<strong>and</strong>l<strong>in</strong>g strategy <strong>in</strong> place that enables you to avoid errors<br />

when possible, <strong>and</strong> that h<strong>and</strong>les them gracefully <strong>and</strong> optionally logs relevant <strong>in</strong>formation for you<br />

when they occur.<br />

The follow<strong>in</strong>g section deals with detect<strong>in</strong>g <strong>and</strong> h<strong>and</strong>l<strong>in</strong>g errors, or exceptions <strong>in</strong> .<strong>NET</strong> parlance;<br />

later <strong>in</strong> this chapter, you learn how to log errors <strong>and</strong> shield your users from ugly pages with detailed<br />

exception messages.<br />

Catch<strong>in</strong>g <strong>and</strong> H<strong>and</strong>l<strong>in</strong>g Exceptions<br />

Normally, when serious exceptions occur, the user is presented with an error message. For example,<br />

if you try to send a message to a mail server that isn’t up <strong>and</strong> runn<strong>in</strong>g, or doesn’t allow you to connect<br />

to it, you’ll get an exception that provides details about the error. The exception that you get<br />

is an <strong>in</strong>stance of a class that ultimately <strong>in</strong>herits the System.Exception class; the base class for all<br />

exceptions. Many exception types exist, each serv<strong>in</strong>g a dist<strong>in</strong>ct purpose.<br />

By default, this exception bubbles up all the way to the user <strong>in</strong>terface where it’s presented as a socalled<br />

Yellow Screen of Death, a reference to W<strong>in</strong>dows’ "Blue Screen of Death” that you get when<br />

W<strong>in</strong>dows crashes. You see a real example of this error <strong>in</strong> the next exercise.<br />

Obviously, it’s a lot better if you can anticipate the exception <strong>and</strong> write some code that prevents it<br />

from appear<strong>in</strong>g <strong>in</strong> the user <strong>in</strong>terface. You could, for example, display a nicely formatted message to<br />

users <strong>in</strong>stead, <strong>in</strong>form<strong>in</strong>g them the message could not be sent at this moment.<br />

Fortunately, support for these k<strong>in</strong>ds of scenarios is <strong>in</strong>tegrated deeply <strong>in</strong> the .<strong>NET</strong> programm<strong>in</strong>g<br />

languages such as C# <strong>and</strong> Visual Basic .<strong>NET</strong>. In these languages, you can make use of Try Catch<br />

F<strong>in</strong>ally blocks (try catch f<strong>in</strong>ally <strong>in</strong> C#) where code that could potentially throw an exception

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

Saved successfully!

Ooh no, something went wrong!