Quit VBScript On Error

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.


MDauphinais1Author Commented:
Hmm..... ok.  Thanks for the help.
Al HollmotzCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.