Server.GetLastError方法

GetLastError方法返回了一个AspError对象,描述了已发生的错误的条件。只有在asp把任何内容发送到客户端之前可用该方法。

GetLastError(
)

参数

该方法没有参数。

返回值

An ASPError Object.

示例代码

下面的代码示例演示了一个网页,你可以把它配置为你的web网站的默认ASP错误处理页。

Note:
欲进一步了解如何配置一个自定义ASP错误页,请参阅启用ASP错误处理主题。
<%@Language="VBSCRIPT"%>
<%
  Option Explicit
  On Error Resume Next
  Response.Clear
  Dim objError
  Set objError = Server.GetLastError()
%>
<html>
<head>
<title>ASP 500 Error</title>
</head>
<body>

<h2 align="center">ASP 500 Error</h2>

<p align="center">An error occurred processing the page that you requested.<br>
Please see the error 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>

下面的三个示例演示了不同的错误,它们生成了500;100自定义错误。错误的三种类型是:

  • 预处理错误
  • 脚本编译错误
  • 运行时错误

下面的示例演示了一个预处理错误,在IIS试图包含该文件时,生成了该错误。至所以会生成该错误,是因为#include语句丢失了针对#include语句的文件参数。

<!--#include fil=inc.h  --> 
<% 
  response.write "hello" 
%>

下面的示例演示了一个脚本编译错误。脚本引擎不能编译脚本,因为它在一个For...Next循环中丢失了关键词Next

<% 
  dim I 
  for i=1 to 1 
  nxt 
%>

下面的示例演示了一个运行时错误,至所以会发生,是因为脚本试图除以0。

<% 
  dim i,j 
  dim sum 
  sum=0 
  j=0 
  for i=1 to 10 
    sum=sum+1 
  next 
  sum=sum/j 
%>

适用于

Server对象

备注

如果针对一个ASP应用程序定义了一个500;100自定义错误,它可能会引用到一个.asp文件。在这种情况下,如果一个应用程序内部的.asp在运行过程中发生了一个错误,服务器会自动通过Server.Transfer方法转送到该ASP网页。所有的来自正在执行的ASP应用程序的状态信息在处理该错误的.asp文件中都可用。另外,还可以用ASPError对象,所以你可以通过你设置来处理该错误的.asp文件来曝露错误的属性。

默认Web网站被配置为使用文件\iishelp\common\500-100.asp。你既可以使用这个文件来处理ASP错误,也可以创建你自己的文件。如果你想修改用来处理500,100自定义错误的.asp文件,你可以使用IIS管理器。

Note:
如果IIS在处理.asp或应用程序的Global.asa文件的时候遭遇了一个错误,就会生成一个500,100错误。

必备条件

Client: Requires Windows XP Professional, Windows 2000 Professional, or Windows NT Workstation 4.0.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.

Product: IIS

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。