Method to terminate script?

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Method to terminate script?

Re: Method to terminate script?

by Eyal » Thu Oct 20, 2016 1:42 pm

If you don't already have VBScript Language Reference (VBSCRIP5.CHM), you can find its list of errors here:
https://msdn.microsoft.com/en-us/librar ... s.84).aspx
and On Error handling is explained a bit here: https://msdn.microsoft.com/en-us/librar ... s.84).aspx

Re: Method to terminate script?

by rivorson » Thu Oct 20, 2016 1:17 pm

Error numbers can be between 0 and 65535. 0 to 512 are reserved for errors defined by VB. 513-65535 are available for user defined errors, presumably including errors defined by MM. I suggest raising the highest error number possible to terminate your code then check if there was an error that is smaller than that.

Code: Select all

Sub AllowEarlyExit()
    On Error Resume Next
    Dim ErrorNumber as Integer
    MainSub
    ErrorNumber = Err.Number
    If ErrorNumber > 0 And ErrorNumber < 65535 Then Err.Raise ErrorNumber
    MsgBox "MainSub finished"
End Sub

Sub MainSub
    'some code
    Err.Raise 65535 'throws error
    'some more code
End Sub

Re: Method to terminate script?

by chrisjj » Thu Oct 20, 2016 12:31 pm

rivorson wrote:I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:
...
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.
That will hide all the genuine error boxes too.

Is there some way to make the hiding specific to the user error? I can find no Error.Raise number for use error but perhaps it would be safe enough to repurpose an existing rare error number.

Re: Method to terminate script?

by rivorson » Thu Oct 20, 2016 7:11 am

I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:

Code: Select all

Sub AllowEarlyExit()
    On Error Resume Next
    MainSub
    MsgBox "MainSub finished"
End Sub

Sub MainSub
    'some code
    Err.Raise 1 'throws error
    'some more code
End Sub
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.

Re: Method to terminate script?

by chrisjj » Thu Oct 20, 2016 5:08 am

rivorson wrote:I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.
That's the litter I already have and am seeking to eliminate.

Thanks anyway for the suggestion.

Re: Method to terminate script?

by chrisjj » Thu Oct 20, 2016 5:07 am

Eyal wrote:There's the STOP statement but it only works in WScript, not VBscript.

I think the only way to do it is to voluntarily generate an error

Code: Select all

Err.Raise <Number>
or

Code: Select all

x = 10 / 0
It's convenient for debugging but not suitable for public release...
Thanks. Trying it http://i.imgur.com/9o87Iw9.png (there's no <number> for script-special error) I find generates two dialog boxes, so is not as convenient as I'd like.

Re: Method to terminate script?

by rivorson » Thu Oct 20, 2016 3:47 am

I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.

Re: Method to terminate script?

by Eyal » Thu Oct 20, 2016 1:36 am

There's the STOP statement but it only works in WScript, not VBscript.

I think the only way to do it is to voluntarily generate an error

Code: Select all

Err.Raise <Number>
or

Code: Select all

x = 10 / 0
It's convenient for debugging but not suitable for public release...

Method to terminate script?

by chrisjj » Fri Oct 14, 2016 4:51 pm

Is there a method call that will exit my script?

Like VBscript EXIT SUB but exiting the script rather than just the sub.

Top