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.

Exception H<strong>and</strong>l<strong>in</strong>g x 685<br />

In the follow<strong>in</strong>g exercise, you see how to use Try Catch F<strong>in</strong>ally <strong>in</strong> your code.<br />

TRY IT OUT<br />

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

In this Try It Out, you see how to write exception-h<strong>and</strong>l<strong>in</strong>g code to catch problems with send<strong>in</strong>g<br />

e-mail. These problems may occur when the mail server is down, for example. To simulate a broken<br />

mail server, you’ll temporarily use a nonexistent mail server name caus<strong>in</strong>g your code to crash.<br />

You’ll try out the Try Catch F<strong>in</strong>ally code <strong>in</strong> a separate page <strong>in</strong> the Demos folder so you can closely<br />

watch its behavior. When you underst<strong>and</strong> how it works, you’ll modify the ContactForm.ascx user<br />

control <strong>and</strong> <strong>in</strong>corporate the exception-h<strong>and</strong>l<strong>in</strong>g code there. The reason you write this code <strong>in</strong> the demo<br />

page first is that the user control uses an Ajax UpdatePanel that shields users from the dirty details of<br />

an exception by default, mak<strong>in</strong>g it difficult to see what’s go<strong>in</strong>g on.<br />

1. Create a new file <strong>in</strong> the Demos folder <strong>and</strong> call it ExceptionH<strong>and</strong>l<strong>in</strong>g.aspx. Base the page on your<br />

custom template <strong>and</strong> set its Title to Exception H<strong>and</strong>l<strong>in</strong>g Demo.<br />

2. Add a Label control to the ma<strong>in</strong> content area <strong>and</strong> set its ID to Message.<br />

3. Switch to Design View <strong>and</strong> set up an event h<strong>and</strong>ler for the Load event of the page by double-click<strong>in</strong>g<br />

the read-only area of the page. Then at the top of the file, add either an Imports or a us<strong>in</strong>g<br />

statement for the System.Net.Mail namespace:<br />

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

Imports System.Net.Mail<br />

C#<br />

us<strong>in</strong>g System.Net.Mail;<br />

4. Add the follow<strong>in</strong>g code to the event h<strong>and</strong>ler that VS created. Notice how this code is almost identical<br />

to the code you added <strong>in</strong> the ContactForm.ascx user control, so you can save yourself some<br />

typ<strong>in</strong>g by copy<strong>in</strong>g parts of the code from that file. Notice how the code passes "DoesNotExist" to<br />

the SmtpClient’s constructor as an argument for the mail host. This is done deliberately to trigger<br />

an exception.<br />

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

Protected Sub Page_Load(sender As Object, e As EventArgs) H<strong>and</strong>les Me.Load<br />

Dim myMessage As MailMessage = New MailMessage()<br />

myMessage.Subject = "Exception H<strong>and</strong>l<strong>in</strong>g Test"<br />

myMessage.Body = "Test message body"<br />

myMessage.From = New MailAddress("you@example.com")<br />

myMessage.To.Add(New MailAddress("you@example.com"))<br />

Dim mySmtpClient As New SmtpClient("DoesNotExist")<br />

mySmtpClient.Send(myMessage)<br />

Message.Text = "Message sent"<br />

End Sub<br />

C#<br />

protected void Page_Load(object sender, EventArgs e)

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

Saved successfully!

Ooh no, something went wrong!