Search code examples
vbscripterror-handlinggotoonerror

VB GoTo failing compilation


The below script will fail with an error, which I'm ok with. Right now, I'm trying to work out my On Error GoTo <label> syntax, and it is currently failing with the following error.

Line: 2
Char: 16
Error: Syntax Error
Code: 800A03EA
Source: Microsoft VBScript compilation error

Code below:

Sub ComCheck
    On Error GoTo ErrorHandler

    Dim fortis

    Wscript.Echo("Creating COM object.")
    Set fortis = CreateObject("TESTCOM.APPLICATION")

    Wscript.Echo("Write Database name.")
    Wscript.Echo(fortis.Databases[0].Name)

    GoTo ScriptEnd

ErrorHandler:
    Wscript.Echo("-------ERROR OCCURRED------")
    Wscript.Echo("#" + Err.Number + "::" + Err.Description)
    Err.Clear

ScriptEnd:
    Wscript.Echo("Script complete.")

End Sub

ComCheck()

Solution

  • This is one of the differences between VB and VBScript: the latter doesn't support the GoTo <label> syntax. The only two possibilities in VBScript are:

    On Error Resume Next
    

    and

    On Error Goto 0
    

    You use the former to turn off VBScript's own error handling (and presumably handle errors yourself), and the latter to turn on VBScript's error handling (which stops all execution if an error is encountered).