asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

ASP and the Error Handler
By Richard Bundock
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    ASP pages are so easy to put together that sometimes developers have not thought through the problems associated with errors. Error handling can help your application to be more robust. I have often come across commercial sites written in ASP that fail to have any sort of error handling.

    Types of Error

    There are 3 main types of errors:

    • Compile-time errors
      These errors are usually in the syntax of the code and stop the ASP from compiling. You may have experienced this if you left the closing ”Next” statement off of a “For” loop.
    • Runtime errors
      These happen when you try to execute the ASP page. For example, if you try setting a variable outside its allowed range.
    • Logic errors
      Logic errors are harder to detect. The problem lies within the structure of the code, and the computer cannot detect an error. These types require thorough testing before rolling out the application.
    As compile-time errors are always trapped and logic errors are only found through thorough testing. This leaves us to worry only about runtime errors. These can stop the execution of your page and leave the user with a lot of non-user-friendly text on the screen.

    Crashing Through

    So how do we handle runtime errors in ASP? Let’s start by using the only command that ASP has to help us - On Error Resume Next. The documentation tells us that:

    “If you don't use an On Error Resume Next statement, any runtime error that occurs is fatal; that is, an error message is displayed and execution stops.”

    This is the key. Runtime error stop the page execution and you get a nasty non-user-friendly message like:

     
     Microsoft OLE DB Provider for ODBC Drivers error 80004005 
     [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 
     /test.asp, line 60 
     
     
    So, with the On Error Resume Next function set at the top of the page, all errors are ignored and execution continues with the next line after the error. That’s all very well and good, but the user does not see any error. This can be even more frustrating when the results appear to not conform to expectations. To avoid this, you need to handle the error at some point within the page.

    Handling the Error

    In ASP, the best way to handle errors is to place code at the bottom of each page that can display an appropriate message to the user. I also recommend using the buffer on every page. If an error occurs, the contents of the page can be cleared before displaying error details. This should be less confusing for the user and you. Here is some sample code:

     
     	<%@ LANGUAGE="VBScript" %>
     
     <% 	‘ Turn on page buffering
     Response.Buffer = True 
     
     ‘ Turn On Error Handling
     On Error Resume Next
     
     ‘ Your ASP Page code
      
     %>	
     
     	<%	‘ Error Handler
     		If Err.Number <> 0 Then
     
     			‘ Clear response buffer
     			Response.Clear
     			
     			‘ Display Error Message to user	%>
     
     		<HTML>
     
     		<HEAD>
     		<TITLE></TITLE>
     		</HEAD>
     
     		<BODY BGCOLOR="#C0C0C0">
     
     		<FONT FACE="ARIAL">An error occurred in the execution of this ASP page<BR>
     		Please report the following information to the support desk<P>
     <B>Page Error Object</B><BR>
     Error Number <%= Err.Number %><BR>
     Error Description <%= Err.Description %><BR>		
     			Source <%= Err.Source %><BR>
     			LineNumber <%= Err.Line %><BR>
     
     	</FONT>
     
     	</BODY>
     	</HTML>
     
     <%	End If
     
     	%>
     
     
    As you can see from above, I first set the On Error Resume Next so that errors don’t stop page execution. Once the execution point falls to the Error Handler I clear the page from memory and return a complete error page to the user. This can say anything. It doesn’t have to offer a printout of the error object or ask the user to contact the support desk. You could of course add some code to log the error in a file or a database.

    Error Handling and Databases

    Adding a database to the error-handling equation can complicate things. Assume that we have an ASP page where a couple of calls are made to a database to display some data, but then an insert/update query is executed at the bottom of the page. Now, because we have the On Error Resume Next switched on, if an error occurs in the select queries, the insert/update will still fire. This can cause data integrity problems within the database or fail to give the desired functionality. To prevent this, a check for an error must be made before any insert/update/delete queries are fired. To achieve this, wrap the database call up. It would look something like this:

     
     	If Err.Number = 0 And objConnection.Errors.Count = 0 Then
     
     		‘ Fire the database query, because there are no errors
     		Set rstResults = dbData.Execute(txtSql)
     
     	End If
     
     

    More Advanced Error Page

    You can also print out more information if an error occurs when execution reaches the bottom of the page, including the page error object properties and the database connection error object properties. This will offer more details on exactly what errors have been returned from the database connection. You will need to add the following to the error page code used above:

     
     <%	‘ Error Handler
     	If Err.Number <> 0 Then
     
     	‘ Clear response buffer
     		Response.Clear
     
     		'	Action sensitive to error
     	Select Case Err.Number
     
     		Case “”		'	Specific error messages
     		'	Placeholder for specific error message code
     ‘	You can handle custom errors here	
     
     Case Else		'	General Error Response
     
     		If IsObject(objConnection) Then
     	
     				If objConnection.Errors.Count > 0 Then	%>
     			
     <B>Database Connection Object</B>
     
     <%					For intLoop = 0 To objConnection.Errors.Count - 1	%>
     				
     Error No: <%= objConnection.Errors(intLoop).Number %><BR>
     Description: <%= objConnection.Errors(intLoop).Description %><BR>
     Source: <%= objConnection.Errors(intLoop).Source %><BR>
     SQLState: <%= objConnection.Errors(intLoop).SQLState %><BR>
     NativeError: <%= objConnection.Errors(intLoop).NativeError %><P>
     
     <%					Next
     				End If
     
     		End If
     
     			If Err.Number <> 0 Then	%>
     
     <B>Page Error Object</B><BR>
     Error Number <%= Err.Number %><BR>
     Error Description <%= Err.Description %><BR>		
     				Source <%= Err.Source %><BR>
     				LineNumber <%= Err.Line %><P>
     
     <%			End If
     
     		End Select
     End If
     %>
     
     
    The code above also allows for more than one error in the database connection object, which there often is. It just loops through the error collection in the database connection object. You will also notice that a Select Case statement allows you to handle a specific page error rather than having you jump into the generic error response.

    Redirects with the Error Handler

    One more thing to watch out for is redirecting from the page before the execution point reaches the error handler. If a redirect happens, then the Error Handler is rendered useless. So you need to wrap any code that redirects, just like you did for the database calls. Here is an example:

     
     	If Err.Number = 0 And objConnection.Errors.Count = 0 Then
     
     		‘ OK to redirect
     		Response.Clear
     		Response.Redirect “<URL Here>”
     
     	End If
     
     

    Making the Code Neater

    To make the code in your application neater, first make the error page an Include file. You can then drop this into any page easily. You will have to follow some rules, however, while your are developing your code. Points to remember when implementing error handling are:

    • Add On Error Resume Next to your page. (Make it the first command after the language declaration.)
    • Always wrap your database calls with checks on both error objects.
    • Always wrap any redirects with checks on both error objects.
    • Make sure you include the error-handler code at the bottom of the page.
    I hope this article helps you to implement strong error handling in ASP pages.

    About the Author

    Richard Bundock has been consulting on Active Server Page issues for more than two years. He is now a freelance consultant specializing in ASP and VB. A certified Microsoft Professional, Richard is currently working on one of the largest ASP Intranet developments in the UK. He hopes to gain his MCSD and MSCE during the summer. His email address is rbundock@deepdisc.co.uk .

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    CustomError 2.0 for IIS
    When errors occur on a Web site, they should be handled in a way that helps the user to get back on track. Unfortunately, setting up customized error pages in IIS usually requires something many Web developers lack -- access to and familiarity with the Web server's administrative interface. With CustomError for IIS, developers can add error pages, coded by hand or created in their favorite editor, by simply uploading them to a designated directory. No administrator intervention is required.
    [Top]
    Other Articles
    Sep 22, 2004 - Unit Test - Testing with NUnit Framework
    Kamran Qamar introduces unit testing with NUnit and offers some best practices, tips, and tricks.
    [Read This Article]  [Top]
    Aug 10, 2004 - Implementing and Promoting Daily Builds
    Automatic daily builds is a well known software engineering best practice. This article introduces a strategy for implementing and promoting daily builds and offers tips and tricks for preventing and fixing breaks.
    [Read This Article]  [Top]
    Jun 21, 2004 - Using Open Source .NET Tools for Sophisticated Builds
    Building an application can be more than pressing F5. With an increasing number of quality packages being released, developers for the .NET platform now have options to create a very sophisticated build process. Aaron Junod describes a sample build environment and shows how a number of tools can work together to make reliable, predictable, and value-added builds.
    [Read This Article]  [Top]
    Jun 18, 2003 - Online Database Functions Testing Tool
    This short article provides source code for a classic ASP online database functions testing application and shows how to configure and use the tool for either SQL Server or Oracle.
    [Read This Article]  [Top]
    Jan 2, 2003 - Web Application Error Handling in ASP.NET
    One of many improvements ASP.NET brings to the development table is in error handling. Adam Tuliper whips up a simple ASP.NET solution for handling those pesky and unexpected post-production errors.
    [Read This Article]  [Top]
    Sep 10, 2002 - Tracing in .NET and Implementing Your Own Trace Listeners
    Mansoor Ahmed Siddiqui explains debugging and tracing and shows how to create custom trace listeners to help ensure hassle-free development.
    [Read This Article]  [Top]
    Sep 5, 2001 - Firing Events in a Shared Hosting Environment
    Firing events on a Web server is an easy task. However most of the easy solutions require you to have your own dedicated IIS or SQL Server on the Internet to play with, a privilege not shared by many. In this article, Matthew Muller shows you how to get the same functionality in a shared hosting environment.
    [Read This Article]  [Top]
    May 25, 2001 - Avoiding a Type Mismatch Error When Using ByRef with ASP and COM
    Unlike programming inside a complete VB system, when using ByRef with ASP and COM, a complication arises because ASP's VBScript is not typed, but the component's VB is typed. This article will briefly explain how ByRef can be used with ASP and COM.
    [Read This Article]  [Top]
    Apr 18, 2001 - Error Reporting - IIS 5.0
    The script in Mark Newlands' article this week handles how errors are displayed and logged. It can capture all values in use at the time (e.g. form, querystring, session,and application level) and records them if you set a Boolean value to do so - displays custom HTML if required. Sends email, logs to database, and/or text file.
    [Read This Article]  [Top]
    Mar 12, 2001 - Transact-SQL Improves Database Error-Handling
    Transact-SQL provides developers with several database error-handling methods. Use these functions to efficiently handle database errors and add an extra level of data validation. This article discusses the @@ERROR, SP_ADDMESSAGE, and RAISERROR functions and provides examples on how to implement them.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers