advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Sidebar 1: web.config
Sidebar 2: global.asax
Partners & Affiliates
advertisement
advertisement
CoDe Magazine
Subscribe to CoDe Magazine
 

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

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
rror handling, paying taxes, and root canals. Each one conjures up a number of negative mental images—some real, some imagined. While I cannot do much to ease the pain of paying taxes or reduce the suffering you might experience during a root canal, this article will enlighten you regarding the ins and outs of error handling in ASP.NET.

Errors you need to concern yourself with fall into two distinct error types—handled errors and unhandled errors. A handled error occurs within your application—you are ready for it and have code in place to take action upon it. For example, maybe you have coded your application to divide one number by another. What if the second number is 0? A divide by zero error will occur and hopefully you will have code in place to handle such a problem. Unhandled errors are those errors that you cannot foresee happening. An example is an incorrectly typed URL reference for a page on your Web site that results in a "404 Page Not Found" error. Hopefully you will have the pieces in place that will direct the user to a much cleaner and more refined page than the default "404 Page Not Found" page.

Fast Facts
The cornerstone on which structured error handling is built is the Try...Catch...Finally code block.

Let's start by discussing the language constructs available to handle errors within ASP.NET applications. We will take a look at system exception objects, the Try...Catch...Finally commands, and creating you own Exception objects.

Errors and Exceptions
While the terms error and exception are often used interchangeably, there is a distinct difference between the two. An error happens during the execution of a block of program code and alters the normal program flow, thus creating an Exception object. When the flow of a program is interrupted by an error, the program will search for exception handling code to tell the program how to react. Simply put, an error is an event. That event creates an object called an exception. This Exception object contains information about the error including when and where it occurred.

The <customErrors> section of the web.config file is your last chance to handle an unexpected error.
Unlike throwing a punch, you will often hear the phrase "throwing an exception." This means that an error occurred in a code block and an Exception object was created.

The generic Exception class from which all Exception objects are derived is the System.Exception class. It contains a number of properties designed to give you an easy way to capture and manage information about the Exception object. Table 1 lists System.Exception properties.

Table 1: System.Exception properties

Property


It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?



© Copyright Component Developer Magazine and EPS Software Corp., 2009
advertisement