查看: 6|回复: 0

[文章教程] 处理 VBScript 中的错误

[复制链接]
xuanxiao 发表于 前天 10:08 | 显示全部楼层 |阅读模式
Visual Basic 中使用的方法与 VBScript 中使用的方法没有什么区别。 主要区别在于,VBScript 不支持在标签上继续执行错误处理的概念。 换句话说,无法在 VBScript 中使用 On Error GoTo。 请改用 On Error Resume Next,然后检查 Err.Number 和 Count 属性的 Errors 集合,如以下示例所示:
  1. <!-- BeginErrorExampleVBS -->  
  2. <HTML>  
  3. <HEAD>  
  4. <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">  
  5. <TITLE>Error Handling Example (VBScript)</TITLE>  
  6. </HEAD>  
  7. <BODY>  
  8.   
  9. <h1>Error Handling Example (VBScript)</h1>  
  10.   
  11. <%  
  12.   
  13.    Dim cnn1  
  14.    Dim errLoop  
  15.    Dim strError  
  16.   
  17.    On Error Resume Next  
  18.   
  19.    ' Intentionally trigger an error.  
  20.    Set cnn1 = Server.CreateObject("ADODB.Connection")  
  21.    cnn1.Open "nothing"  
  22.   
  23.    If cnn1.Errors.Count > 0 Then  
  24.       ' Enumerate Errors collection and display  
  25.       ' properties of each Error object.  
  26.       For Each errLoop In cnn1.Errors  
  27.          strError = "Error #" & errLoop.Number & "<br>" & _  
  28.             "   " & errLoop.Description & "<br>" & _  
  29.             "   (Source: " & errLoop.Source & ")" & "<br>" & _  
  30.             "   (SQL State: " & errLoop.SQLState & ")" & "<br>" & _  
  31.             "   (NativeError: " & errLoop.NativeError & ")" & "<br>"  
  32.          If errLoop.HelpFile = "" Then  
  33.             strError = strError & _  
  34.                "   No Help file available" & _  
  35.                "<br><br>"  
  36.          Else  
  37.             strError = strError & _  
  38.                "   (HelpFile: " & errLoop.HelpFile & ")" & "<br>" & _  
  39.                "   (HelpContext: " & errLoop.HelpContext & ")" & _  
  40.                "<br><br>"  
  41.          End If  
  42.   
  43.          Response.Write("<p>" & strError & "</p>")  
  44.       Next  
  45.    End If  
  46.   
  47. %>  
  48.   
  49. </BODY>  
  50. </HTML>  
  51. <!-- EndErrorExampleVBS -->
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号