This step-by-step guide demonstrates how to use the new ASPError object to create a custom, centralized error-handling page. For
example, your application may require that someone be sent an e-mail
notification when an error has occurred and some process in the system has not
completed. Or, you may want to log some information to a database in addition
to the Internet Information Server (IIS) error log information. For information
on how to do this, see the "Error Reporting - IIS 5.0" article in the
"References" section.
Microsoft Windows 2000 Professional, Windows 2000 Server,
or Windows 2000 Advanced Server
Internet Information Server 5.0 installed and
configured
The ASPError object and the related Active Server Pages (ASP) methods that are
required to use the ASPError object are new to Active Server Pages 3.0 (ASP) and are not
available on pre-Windows 2000 operating systems.
Right-click Start, and then click Explore to open Windows Explorer.
Select the root folder of your Web site (which is
C:\InetPub\Wwwroot by default).
On the File menu, point to New, and then click Folder. Name the folder ErrTest.
Right-click ErrTest, and then click Properties.
On the Security tab, click Everyone. Under Permissions, make sure that the Read and Write check boxes are selected. Leave the other check boxes as they
are. If you make any changes, click Apply.
From the Windows Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager to open the Internet Services Manager (ISM).
Under the Default Web Site, you should see the ErrTest directory. Right-click ErrTest, and then click Properties.
Confirm that Anonymous access is enabled as follows:
On the Directory Security tab, under Anonymous Access and Authentication control, click Edit.
Select the Anonymous Access check box, click OK, and then click Apply.
To mark the directory as an ASP application, on the Directory tab, click Create under Application Settings. Click Apply to save your changes, and click OK to close the ErrTest Properties dialog box.
From the Windows Start menu, point to Programs, point to Accessories, and then click Notepad.
Paste the following code into Notepad to create a sample
ASP error-handler page:
<%@Language="VBSCRIPT"%>
<%
Option Explicit
On Error Resume Next
Response.Clear
Dim objError
Set objError = Server.GetLastError()
%>
<html>
<head>
<title>ASP 500 Error</title>
<style>
BODY { FONT-FAMILY: Arial; FONT-SIZE: 10pt;
BACKGROUND: #ffffff; COLOR: #000000;
MARGIN: 15px; }
H2 { FONT-SIZE: 16pt; COLOR: #ff0000; }
TABLE { BACKGROUND: #000000; PADDING: 5px; }
TH { BACKGROUND: #0000ff; COLOR: #ffffff; }
TR { BACKGROUND: #cccccc; COLOR: #000000; }
</style>
</head>
<body>
<h2 align="center">ASP 500 Error</h2>
<p align="center">An error occurred processing the page you requested.<br>
Please see the details below for more information.</p>
<div align="center"><center>
<table>
<% If Len(CStr(objError.ASPCode)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">IIS Error Number</th>
<td align="left" valign="top"><%=objError.ASPCode%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Number)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">COM Error Number</th>
<td align="left" valign="top"><%=objError.Number%>
<%=" (0x" & Hex(objError.Number) & ")"%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Source)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Error Source</th>
<td align="left" valign="top"><%=objError.Source%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.File)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">File Name</th>
<td align="left" valign="top"><%=objError.File%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Line)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Line Number</th>
<td align="left" valign="top"><%=objError.Line%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Description)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Brief Description</th>
<td align="left" valign="top"><%=objError.Description%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.ASPDescription)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Full Description</th>
<td align="left" valign="top"><%=objError.ASPDescription%></td>
</tr>
<% End If %>
</table>
</center></div>
</body>
</html>
On the File menu, click Save. When you are prompted, name the page
My500ErrHandler.asp, and save the page to the ErrTest
folder that you created earlier.
Set the New Page as the Error Handler for 500-Type Errors
On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager.
Right-click the ErrTest virtual directory under the Default
Web Site, and then click Properties.
On the Custom Errors tab, scroll down to an entry that includes "500;100" in the HTTP
Errors column. This is where you change the setting to point to your custom
error handler.
Select the line that is the current definition for 500;100
errors, and then click Edit Properties.
In the Message Type drop-down list box, click URL.
In the URL text box, notice that it points to the page at
"/iisHelp/common/500-100.asp" by default. This is the default error-handling
page on your system, which normally handles ASP errors if you do not define a
custom handler. Note that you can open this page in a text editor to examine
its error-handling techniques.
To enable your custom handler, type the virtual path to
your ASP page. For example, if your ErrTest virtual directory is immediately
beneath the root Web site on your server, type the following path:
/ErrTest/My500ErrHandler.asp
Click OK twice to close the dialog boxes and set your custom
error-handler.
The following page illustrates a run-time error, which you
receive when you perform an operation that is impossible to complete, such as
division by zero:
Click Start, point to Programs, point to Accessories, and then click Notepad.
On the File menu, click Save. When you are prompted, name the page
BadPage1.asp, and save the page to the ErrTest folder
that you created earlier.
In your Web browser, type the following URL in the Address
bar to browse to the page:
http://<Your_Server_Name>/ErrTest/BadPage1.asp
Test Two: Invalid Method in Code
The following page illustrates what happens if the code calls a
method that does not exist or is misspelled in the reference to one of the
intrinsic ASP objects:
From the Start menu, point to Programs, point to Accessories, and then click Notepad.
When you are building a custom error handler, you should be
aware of two additional ASP concepts: the Server.Transfer method and the Server.GetLastError method. The Server.Transfer method transfers execution to the error-handling page that is
defined in your application. Because Server.Transfer occurs automatically, normally you do not have to deal with it.
The Server.GetLastError method is used to return the ASPError object in your error-handling page. You do not have to use this
method, and it is included in the preceding sample error-handling page. For
more information about both methods, see the "References" section.
The built-in error handler can handle errors regardless of
whether the page that causes the error is written in JScript or VBScript;
however, the basic sample in this article only returns meaningful information
about errors that come from VBScript pages. If your page is written in JScript,
you may need to build an error handler that is specific to JScript; or, if you
need your error handler to manage errors from any type of page, study the
default error-handling page to see the techniques that are used.
Microsoft Internet Explorer 5 or later includes a setting
that may override standard HTTP Web server error messages, which can lead to
unexpected behavior in your browser. To turn off this feature, on the Tools menu in your browser, click Internet Options. On the Advanced tab, clear the Show Friendly HTTP Error Messages
check box.
Also remember that you can still use inline error handling,
and inline error handling overrides the centralized error handler. To
demonstrate this, open BadPage1.asp in Notepad, and paste the following
modified code, which adds an inline error handler:
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 1</title>
</head>
<body>
<%
On error resume next
Response.Write 1/0
if err.number <> 0 then
Response.Write err.description & "<BR>" & err.source & "<BR>"
err.clear
end if
%>
</body>
</html>
Save these changes, and browse to the file in your Web browser. Notice
that your inline error handler overrides the centralized error handler that you
created.
NOTE: The MSDN documentation that is listed above is also included
with IIS 5.0 and is installed on your system (typically in the
http://localhost/iishelp folder).
Need More Help? Contact a Support professional by E-mail, Online or Phone.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.