ASP Err Object / Error Handling
There are many reasons to use error handling on your ASP pages.
With error handling you can log errors to a file, send them to someone via
email, or display a message to the user about the error that has occurred.
Error handling in VBScript is relatively primitive. There are basically
just two options:
On Error Goto 0 (the default)
This causes execution to break when an error happens and show an error
message.
On Error Resume Next
This causes the script to continue execution starting with the next line
after the error.
When "On Error Resume Next" is enabled, the properties of the Err object are
automatically populated with details of the errors that have occurred.
The code below is an example of how to use the ASP err object to do error
handling.
<%
' Tell our script to continue executing even if an error occurs:
On Error Resume Next
' Some code that is intentionally wrong:
Dim cnnTest
Set cnnTest = Server.CreateObject("ADODB.ThisObjectDoesntExist")
Set cnnTest = Nothing
%>
<p>
Error details:
</p>
<table border="1">
<tr>
<td>Err.Source</td>
<td><%= Err.Source %></td>
</tr>
<tr>
<td>Err.Number</td>
<td><%= Err.Number %></td>
</tr>
<tr>
<td>Err.Description</td>
<td><%= Err.Description %></td>
</tr>
</table>
<%
' You can clear the error information in the Err object by calling
' the clear method:
Err.Clear
' You can also raise an error of your own if you need to:
Err.Raise 1, "My Error", "This is a custom error that I raised!"
%>
<p>
Error details:
</p>
<table border="1">
<tr>
<td>Err.Source</td>
<td><%= Err.Source %></td>
</tr>
<tr>
<td>Err.Number</td>
<td><%= Err.Number %></td>
</tr>
<tr>
<td>Err.Description</td>
<td><%= Err.Description %></td>
</tr>
</table>
<%
' Turn "error-handling" back off... after the next line the
' script will break as normal if any errors happen.
On Error Goto 0
%> |
Starting with ASP 3.0 there is a another method for handling run-time errors: the ASPError object. It's used to centrally handle errors for an entire site by setting up a custom error page. You can find more information about this type of error handling here:
http://www.asp101.com/articles/carvin/errlog/default.asp http://www.asp101.com/articles/stuart/asperrormsgs/default.asp
|