代码之家  ›  专栏  ›  技术社区  ›  apandit

VBScript-使用错误处理

  •  76
  • apandit  · 技术社区  · 18 年前

    我想使用VBScript捕获错误并记录它们(即在错误“log something”上),然后继续脚本的下一行。

    例如

    On Error Resume Next
    'Do Step 1
    'Do Step 2
    'Do Step 3
    

    当步骤1发生错误时,我希望它记录该错误(或使用它执行其他自定义功能),然后在步骤2继续。这可能吗?我如何实现它?

    编辑:我可以这样做吗?

    On Error Resume myErrCatch
    'Do step 1
    'Do step 2
    'Do step 3
    
    myErrCatch:
    'log error
    Resume Next
    
    3 回复  |  直到 18 年前
        1
  •  164
  •   Dylan Beattie    9 年前

    VBScript没有抛出或捕获异常的概念,但运行时提供了一个全局Err对象,其中包含上次执行的操作的结果。每次操作后,必须显式检查Err.Number属性是否为非零。

    On Error Resume Next
    
    DoStep1
    
    If Err.Number <> 0 Then
      WScript.Echo "Error in DoStep1: " & Err.Description
      Err.Clear
    End If
    
    DoStep2
    
    If Err.Number <> 0 Then
      WScript.Echo "Error in DoStop2:" & Err.Description
      Err.Clear
    End If
    
    'If you no longer want to continue following an error after that block's completed,
    'call this.
    On Error Goto 0
    

    Visual Basic和Visual Basic for Applications(VBA)支持“On Error Goto[label]”语法,但VBScript不支持此语言功能,因此您必须按上述说明使用On Error Resume Next。

        2
  •  12
  •   omegastripes    11 年前

    On Error Resume Next 不是全局设置的。您可以将代码的不安全部分放入一个函数中,如果发生错误,该函数将立即中断,并从子函数调用此函数 OERN 陈述

    ErrCatch()
    
    Sub ErrCatch()
        Dim Res, CurrentStep
    
        On Error Resume Next
    
        Res = UnSafeCode(20, CurrentStep)
        MsgBox "ErrStep " & CurrentStep & vbCrLf & Err.Description
    
    End Sub
    
    Function UnSafeCode(Arg, ErrStep)
    
        ErrStep = 1
        UnSafeCode = 1 / (Arg - 10)
    
        ErrStep = 2
        UnSafeCode = 1 / (Arg - 20)
    
        ErrStep = 3
        UnSafeCode = 1 / (Arg - 30)
    
        ErrStep = 0
    End Function
    
        3
  •  5
  •   Cid Gordon    7 年前

    您可以在facade函数中重新组合步骤函数调用:

    sub facade()
        call step1()
        call step2()
        call step3()
        call step4()
        call step5()
    end sub
    

    然后,将错误处理放在调用facade的上层函数中:

    sub main()
        On error resume next
    
        call facade()
    
        If Err.Number <> 0 Then
            ' MsgBox or whatever. You may want to display or log your error there
            msgbox Err.Description
            Err.Clear
        End If
    
        On Error Goto 0
    end sub
    

    现在,让我们假设 step3() 引发一个错误。自从 facade() 不处理错误(存在错误) 不 On error resume next 在里面 门面() ),错误将返回到 main() step4() step5() 不会被处决。

    您的错误处理现在在1个代码块中重构

        4
  •  0
  •   MistyDawn    7 年前

    我对VBScript非常陌生,因此这可能不是最佳实践,或者可能有一个原因我还不知道不应该这样做,但这是我提出的解决方案,可以减少主代码块中的错误日志记录代码量。

    Dim oConn, connStr
    Set oConn = Server.CreateObject("ADODB.Connection")
    connStr = "Provider=SQLOLEDB;Server=XX;UID=XX;PWD=XX;Databse=XX"
    
    ON ERROR RESUME NEXT
    
    oConn.Open connStr
    If err.Number <> 0 Then : showError() : End If
    
    
    Sub ShowError()
    
        'You could write the error details to the console...
        errDetail = "<script>" & _
        "console.log('Description: " & err.Description & "');" & _
        "console.log('Error number: " & err.Number & "');" & _
        "console.log('Error source: " & err.Source & "');" & _
        "</script>"
    
        Response.Write(errDetail)       
    
        '...you could display the error info directly in the page...
        Response.Write("Error Description: " & err.Description)
        Response.Write("Error Source: " & err.Source)
        Response.Write("Error Number: " & err.Number)
    
        '...or you could execute additional code when an error is thrown...
        'Insert error handling code here
    
        err.clear
    End Sub