ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> ASP Articles -> ASP Error Handling - On Error Resume Next

ASP Error Handling - On Error Resume Next

In ASP you handle application errors by using the On Error resume Next statement.
If you don’t want your visitors to see ugly meaningless error messages, then you’ll have to implement some kind of error handling in your ASP applications.


How does ASP Error Handling work?

The first think you need to do is to put the following line on the top of your ASP page:

<%
On Error resume Next
%>

This line simply tells the ASP interpreter, to continue with the executing of the ASP script if there is an error, instead of throwing exception and stopping the script execution.

So what will happen if you follow my advice and add the On Error Resume Next line to your page? Every error generated by your ASP page will be ignored, and as far as your users are concerned everything will look fine. The question is, would everything be OK really and the answer is NO!

Consider the following common web application. We have a simple user registration form, which is saved in our SQL Server database upon submission. If the SQL Server is down at the moment the user submits his form, the ASP script will attempt to generate an ADO connection error, but because we have put the On Error Resume Next statement at the top of our script, this error will be ignored. Hmm, that’s no good. The user will think that his registration form went through, but in fact his registration detail has not being saved.


How does ASP error trapping and handling works?

So far we only told the ASP interpreter to ignore all error messages, but we haven’t done anything to actually trap & handle our errors. To trap & handle errors in an ASP application we need to use the Err object and 2 of his properties – Err.Number and Err.Description.

<%
If Err.Number <> 0 then
HandleError Err.Description
Error.Clear
End If
%>

If the Err.Number value is not 0 then we know that there has been an error in our application and we call a custom subroutine called HandleError passing it the error message as a parameter.

You can do several things in the HandleError subroutine like, send email to the webmaster with the error message, write the error to a log file, display a friendly error messages to the user, and stop the execution of the APS page.

<%
Sub HandleError
‘ Send email notifying the webmaster of the site about the error
‘ Write the error message in a application error log file
‘ Display friendly error message to the user
‘ Stop the execution of the ASP page
End Sub
%>

The On Error GoTo 0 statement is used to disable any error handling.

Error handling allows you to display friendly error messages for the end users and at the same time it helps you debug your ASP application.




Contact Us