Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Errors In Your ASP.NET Code? Don't Throw a Fit, Throw an Exception! : Page 2

Even the best designed applications need to properly manage errors—both the errors you can plan for and those you cannot. In this article, you'll learn error handling techniques in ASP.NET. Topics will range from handling common errors with the Try...Catch syntax to logging unhandled errors into the Windows Event Log.


advertisement
Try...Catch...Finally Block
The cornerstone upon which structured error handling is built is the Try...Catch...Finally code block. This control structure tests a block of code and if an error occurs, throws an exception.

The Try command signifies the beginning of a block of code. If an error occurs during the execution of the Try block of code, an exception object is created and the appropriate Catch line handles the exception. If the code in the Try command executes without error, the optional Finally code runs. The Finally code executes regardless of whether or not an exception was thrown. It will also execute after an Exit Try and Exit Sub.

Try 'Code to try and run Catch ExceptionObject As Exception [Filters] 'Handle the problem here Finally 'Tie up any loose ends here like closing 'a database connection End Try

Catch identifies one or more exceptions that may have been thrown. You can use any number of Catch statements, each taking one of three possible forms: Catch, Catch...As, and Catch...When. If an exception does occurs, the Catch statements are evaluated in the order they appear within the Try...Catch...Finally code block, so you should always list your Catch clauses in order, from the more specific down to the more general. A good programming practice is to always use the following as the last Catch statement in a group of Catch statements.

Catch ex As Exception 'Code to handle the exception here

Optionally, the Catch statement can include a When clause. The When clause is followed by a Boolean expression, and the exception is only handled if the When expression evaluates to True. The opposite is NOT true though. Just because the expression evaluates to true does NOT mean the Catch code will run. In the following example, if the user enters 20 in the txtCustomerRequest TextBox then intBananas ends up with 0 after the calculation in the Try block.



Dim intBananas As Integer = 20 Try intBananas = intBananas - Me.txtRequest.Text Catch ex As Exception When intBananas = 0 Response.Write("DOH!! Yes, we have no bananas!") Exit Sub Catch ex As Exception When intBananas < 10 Response.Write("Time to order more bananas!") Exit Sub Catch ex As Exception Response.Write("Uh oh... unexpected problem") Exit Sub Finally Response.Write("mmmmmm bananas!!!") End Try

If no error occurs during the calculation in the Try block, the Catch clause with "When intBananas = 0" does NOT fire.

The optional Finally code block is always the last code to be executed as control leaves a Try...Catch...Finally code block. This is true whether or not an unhandled exception occurs or if an Exit Try statement is encountered. In the revised code, a Finally clause has been added and will fire whether or not a calculation error occurred.

Dim intBananas As Integer = 20 Try intBananas = intBananas - Me.txtRequest.Text Catch ex As Exception When intBananas = 0 Response.Write("DOH!! Yes, we have no bananas!") Exit Sub Catch ex As Exception When intBananas < 10 Response.Write("Time to order more bananas!") Exit Sub Catch ex As Exception Response.Write("Uh oh... unexpected problem") Exit Sub Finally Response.Write("mmmmmm bananas!!!") End Try



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date