【问题标题】:ASP-Classic: Get error description into HTML tagsASP-Classic:在 HTML 标签中获取错误描述
【发布时间】:2014-09-04 06:24:56
【问题描述】:

如何从 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

谢谢

【问题讨论】:

    标签: html asp-classic


    【解决方案1】:

    @SearchAndResQdoesn't mention 是您的代码失败的原因。

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

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

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

    如果您想在错误检查 (If Err.number &lt;&gt; 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
    

    【讨论】:

    • 感谢您的编辑,我认为发布该链接比我解释事情要好。不太会说话:)
    • @SearchAndResQ 我也不只是试一试。
    【解决方案2】:

    通常,脚本会在出现错误时停止执行。要查看错误是什么,您必须在可能引发错误的行之前使用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 中的错误处理:How To Create a Custom ASP Error Handling Page

    【讨论】:

    • 你测试过这个吗?请更清楚。它对我不起作用。
    • 我已经对此进行了测试,它对我有用。哪一部分不清楚?
    • 只是为了扩展这个答案,您还可以在 IIS 中设置自定义错误陷阱来捕获您的错误。 This page 告诉你所有关于在你的 asp 页面中捕获错误并反馈它们的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多