How To Use the ASPError Object to Build a Custom Error-Handling Page in Internet Information Server (299981)
The information in this article applies to:
- Microsoft Active Server Pages
- Microsoft Internet Information Server 5.0
This article was previously published under Q299981 SUMMARY 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.
back to the top
Prerequisites- 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.
back to the top
Create a New SubWeb- 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.
back to the top
Create a Custom Error-Handling ASP Page- 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.
back to the top
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.
back to the top
Test Your Custom Error HandlerTest One: Run-Time Error 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.
- Paste the following code:
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 1</title>
</head>
<body>
<% Response.Write 1/0 %>
</body>
</html>
- 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
back to the top
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.
- Paste the following code:
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 2</title>
</head>
<body>
<% Response.BadMethod "Hello" %>
</body>
</html>
- From the File menu, click Save. When you are prompted, name the page
BadPage2.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/BadPage2.asp
back to the top
Test Three: Unregistered Object The following page illustrates the error that you receive when
you try to create an external object that is not registered on the server:
- From the Start menu, point to Programs, point to Accessories, and then click Notepad.
- Paste the following code:
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 3</title>
</head>
<body>
<%
Dim objBad
Set objBad = Server.CreateObject("BAD.OBJECT.CLASS")
%>
</body>
</html>
- From the File menu, click Save. When you are prompted, name the page
BadPage3.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/BadPage3.asp
back to the top
Troubleshooting
back to the top
REFERENCESMicrosoft Knowledge Base Articles224070 Creating Custom ASP Error Pages
219294 How to Use the Server.Transfer Method
back to the top
MSDN DocumentationNOTE: 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).
back to the top
Third-Party Content
back to the top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbBug kbCodeSnippet kbConfig kbDebug kbGuidelines kbHOWTOmaster kbSample kbScript kbWebServer KB299981 kbAudDeveloper |
---|
|