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()
WScript.Quit
End Function


On Error GoTo 5

5 WScript.Quit
MDauphinais1Asked:
Who is Participating?
 
itdrmsCommented:
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
   WScript.Quit
End If
On Error GoTo 0
0
 
RobSampsonCommented:
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.

Regards,

Rob.
0
 
MDauphinais1Author Commented:
Hmm..... ok.  Thanks for the help.
0
 
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()

    sVBScript_Argument=""
    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!"
        wscript.quit
    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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.