03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

C#<br />

catch (Exception)<br />

{<br />

userMessage = "An unknown error occurred.";<br />

}<br />

The ability to specify an Exception type is useful when you th<strong>in</strong>k your code can encounter more<br />

than one exception. In that case, you can have multiple Catch blocks for different Exception types.<br />

The follow<strong>in</strong>g code is capable of h<strong>and</strong>l<strong>in</strong>g a specific SmtpException that may occur dur<strong>in</strong>g the<br />

mail send<strong>in</strong>g operation, <strong>and</strong> it’s also capable of catch<strong>in</strong>g all other exceptions us<strong>in</strong>g its generic Catch<br />

block:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Try<br />

mySmtpClient.Send(myMessage)<br />

Catch smtpException As SmtpException<br />

userMessage = "Sorry, an error occurred while send<strong>in</strong>g your message."<br />

Catch ex As Exception<br />

' Someth<strong>in</strong>g else went wrong.<br />

End Try<br />

C#<br />

try<br />

{<br />

mySmtpClient.Send(myMessage);<br />

}<br />

catch (SmtpException smtpException)<br />

{<br />

userMessage = "Sorry, an error occurred while send<strong>in</strong>g your message.";<br />

}<br />

catch (Exception ex)<br />

{<br />

// Someth<strong>in</strong>g else went wrong.<br />

}<br />

The order of the exception-h<strong>and</strong>l<strong>in</strong>g blocks is important. .<strong>NET</strong> scans the list of Catch blocks from<br />

top to bottom <strong>and</strong> only fires the code <strong>in</strong> the first block that matches a specific type of exception.<br />

In the preced<strong>in</strong>g example, when an SmtpException occurs (which is a subclass of Exception), it<br />

will be caught by the Catch block that h<strong>and</strong>les exceptions of type SmtpException. Although an<br />

SmtpException is also an Exception, the code <strong>in</strong> the last Catch block won’t be fired anymore<br />

because only the first match<strong>in</strong>g Catch block is h<strong>and</strong>led. Therefore, if you reverse the order of the<br />

Catch blocks <strong>in</strong> this example, the more generic Exception block would be executed, <strong>and</strong> the code <strong>in</strong><br />

the SmtpException block would never run.<br />

WARNING The preced<strong>in</strong>g example shows how to catch all exceptions us<strong>in</strong>g the<br />

base Exception type <strong>in</strong> the Catch block. Don’t use this <strong>in</strong> your own websites.<br />

Instead, h<strong>and</strong>le only those types of exceptions that you know how to deal with,<br />

<strong>and</strong> let all other, unknown exceptions bubble <strong>in</strong> the application. Later <strong>in</strong> this<br />

chapter you see how to centrally h<strong>and</strong>le these unh<strong>and</strong>led exceptions.

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

Saved successfully!

Ooh no, something went wrong!