VB GoTo failing compilation

荒凉一梦 提交于 2019-12-11 11:48:55

问题


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()

回答1:


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).



来源:https://stackoverflow.com/questions/33107723/vb-goto-failing-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!