ASP经典:将错误描述获取到HTML标记中



如何从vb到html获取错误描述?我试过下面的案例,但都不起作用。

代码:

if err then
Response.Write " :Err Information==>>"          
%>
<HTML><table><tr><td bgcolor="#FF0000"><%=err.Description%></td></tr></table></HTML>
<%
End if
On Error Goto 0

感谢

@SearchAndResQ没有提到代码失败的原因。

这里的问题是Classic ASP将停止执行并向客户端返回HTTP 500 Internal Server(取决于服务器的配置方式将取决于响应的详细程度)。

若要在遇到错误或使用Err.Raise()手动引发错误时停止执行,请使用On Error Resume Next。此语句告诉VBScript运行时在遇到错误时跳转到下一行并填充Err对象。

要捕获此错误,请检查Err.Number属性以查看是否引发了错误。完成后,使用On Error Goto 0将错误处理重置为默认状态(错误时停止执行)。

如果要在错误检查(If Err.number <> 0 Then)中测试On Error Resume NextOn Error Goto 0之间的多个错误,请使用Err.Clear()重置Err对象(Err.Number = 0)。

'We are expecting the next statement to sometimes fail so try to trap the error.
On Error Resume Next
' << Statement here you expect to error will be skipped >>
'Check whether error occurred.
If Err.Number <> 0 Then
  'An error occurred, handle it here (display message etc).
  'Error has been handled reset the Err object.
  Call Err.Clear() 'Err.Number is now 0
End If
'Stop trapping errors
On Error Goto 0

通常,脚本会在出现错误时停止执行。要查看错误是什么,您必须在可能引发错误的行之前使用On Error Resume Next,然后检查Err对象

示例:

<%
On Error Resume Next
Dim i : i = 1/0 'division by zero should raise an error
If Err Then
'or you can check 
'If Err.number <> 0 Then
%>
    <table>
    <tr>
        <td>:Err Information==>></td>
        <td bgcolor="#FF0000"><%=err.Description%></td>
    </tr>
    </table>
<%
End If
On Error Goto 0
%>

关于ASP中错误处理的更多信息:如何创建自定义ASP错误处理页面

最新更新