Quit VBScript On Error

Posted on 2007-10-03
Medium Priority
Last Modified: 2016-10-22
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
Question by:MDauphinais1

Accepted Solution

itdrms earned 1000 total points
ID: 20011686
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
LVL 65

Expert Comment

ID: 20011967
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.



Author Comment

ID: 20012855
Hmm..... ok.  Thanks for the help.

Expert Comment

by:Al Hollmotz
ID: 41855605
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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question