Error handling in asp

Read this tip to make your life smarter, better, faster and wiser. LifeTips is the place to go when you need to know about Web Application Development Tips and other Web Development topics.

How do I handle errors in asp?

Error handling in asp

When your code bombs and displays a "Page can't be reached" or and other error page, it's embarrasing. Here's a more elegant way to handle it. Use "On Error Resume Next" and "On Error Goto 0" EFFECTIVELY. First create an error page yourself - one that not only looks better than the standard error page, but that also tells the user something usable.. and include a "go back" button if necessary.

To use the above, write:
On Error Resume Next
...on the line ABOVE where the error would possibly happen, for instance, just before a database call.

Then on the line immediately after the offending line, write:
If err.number <> 0 then response.redirect "yourerrorpage.asp?err=" & err.description
*note: use this redirect ONLY if you want to alert the user of the error. You can omit this if you simply want the user to continue without an error showing up. You will have to find out why the error happened, but that's out of scope with this tip.

After that offending line, write:
On Error Goto 0
-- this resets the error object.

On your error page, at the top (but within the <% %> code block) write:
response.write "Error: " & request.querystring("err") & "
please contact your administrator, or to try again, click here" (originalpage.asp is the page you had the error).

Microsoft has a good resource on this topic.

   

Comments

Nobody has commented on this tip yet. Be the first.



Name:


URL: (optional)


Comment:


Not finding the advice and tips you need on this Web Development Tip Site? Request a Tip Now!


Guru Spotlight
Jennifer Mathes, Ph.D.