Quit VBScript On Error

MDauphinais1 used Ask the Experts™
How can I quit the vbscript on error instead of resume next?

I've tried a few variations and nothing works.

I tried:
On Error GoTo Quit

Function Quit()
End Function

On Error GoTo 5

5 WScript.Quit
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Nope, sorry.  Only 2 options with VBS -- handle each possible error or ignore all errors.  GoTo 0 is the only GoTo option -- it doesn't really mean the same as a GoTo to redirect -- but totally understand what you're attempting.  It just means the opposite of resume next, instead of continuing it throws an ugly error to the user.
There are limitations/tradeoffs for not having a full blown compiler.

The only good thing is that the On Error is not a one time statement.  You can switch back in forth in your script.
If there's a particular place in code you are concerned with you can turn Resume Next on just before and turn it off after, otherwise it gets pretty laborious.  No nice global catches.

On Error Resume Next
AvgResult = a/b
If Err.Number > 0
End If
On Error GoTo 0
Most Valuable Expert 2012
Top Expert 2014

itdrms is right.
Omitting any On Error Resume Statement will stop VBScript continuing, and upon any error, it will crash out with the error message.

The only way to do it in a user friendly manner is to use the code itdrms provided, at a place where you expect an error may occur.




Hmm..... ok.  Thanks for the help.
I know this is 9 years since this post was originally posted and answered but I'm just starting to use VBScript to automate and simplify specific tasks for some COBOL programs I currently use at home.  I used to use DOS batch files, but do to some recent changes, I've needed the benefits of VBScript.

I have the same situation, I want a script to exit no matter where it is if I've encountered an error.  I use the WScript.Quit object.method.

Here's my sample code;

sub Initialize_Variables()

    if wscript.arguments.count <> 1 then
        Msgbox "This script must be run with a single argument." & vbcrlf & _
               vbcrlf & "Valid arguments are;" & vbcrlf & _
               "    MOVE_CSV_FILE | RENAME_TO_YYYYMM", _
               vbok, "SSBHL_File_Maint.vbs Run Error!"
    end if

    sVBScript_Argument = ucase(wscript.arguments.item (0))

end sub

This subroutine is called from the main routine just after I've dimensioned all my variables.  This script should be run with an argument (MOVE_CSV_FILE or RENAME_TO_YYYYMM).  If there's no argument or more than 1, the messagebox appears and the script exits immediately displaying only my user friendly message box.  I have no ON ERROR RESUME NEXT statement.  I'm not using the err object.  I've found that the Exit Sub statement  is causing some syntax errors.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial