Handling ASP Errors
By Stephen Bucaro
ASP will let you know an error occurs, usually by displaying the message "The
page cannot be displayed" along with the code "HTTP 500 - Internal Server Error
- ASP Error - Internet Information Sever" and a terse message giving some usually
not too helpful information about what caused the error. This is NOT the type
of message you want to send to your Web site's visitor.
ASP errors are basically of two types. The first type is "syntax" errors or
"logic" errors caused by the programmer. The second type "run-time" errors
generated by the system. Examples of system errors are a file or database that
ASP can't perform an operation on because of low system resources of a loss of
connectivity.
There are two ways to respond to an error. The first way is to just let them
happen and expose your Web site's visitors to the default error message, or by
"Handling" the error. Handling the error involves determining where an error
might occur in your code, for example in code that accesses a file or database,
and adding code to do something about the error.
Things you might do about an error are display a friendly error message to the
user, perform any clean up tasks required by the error, log the error, or notify
the webmaster of the error.
There are two ways to handle an error. The first way is to use VBScript's "On
Error Resume Next" statement. Below is an example of using the "On Error" statement.
<%
Set cn = CreateObject("ADODB.Connection")
On Error Resume Next
cn.Provider = "sas.localprovider.1"
cn.Properties("Data Source") = "c:\testdata"
cn.Open
cn.Close
%>
This code creates a database connection, configures the connection, and then
opens and closes the connection. If an error occurs when attempting to configure
and use the database connection, the "On Error Resume Next" will cause execution
of the code to continue on the next line after the line that caused the error.
The "On Error Resume Next" stops the default error message from appearing and
allows the webpage to continue execution, but it doesn't do anything about the error.
The second way to handle an error is to use the Err object. The properties
and methods of the Err object are listed below.
Properties
.Number
.Description
.Source
Methods
.Clear
.Raise
|