How to stop execution in VBScript?

Member

by isadore , in category: Other , a year ago

How to stop execution in VBScript?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by craig , a year ago

@isadore 

There are a few different ways to stop the execution of a VBScript program:

  1. WScript.Quit: This method terminates the current script and returns control to the calling script or command prompt. It can also be used to specify an exit code, which is an integer value that is returned to the calling script or command prompt.
  2. Exit: This statement terminates the current script and returns control to the calling script or command prompt. It can also be used to specify the type of exit, such as Exit Do, Exit For, or Exit Function.
  3. Err.Raise: This method generates a runtime error, which will cause the script to terminate execution. You can specify the error number and a description of the error.


Here is an example of how to use WScript.Quit to stop execution and return an exit code:

1
WScript.Quit 123


Here is an example of how to use Exit to stop execution of a For loop:

1
2
3
4
5
6
For i = 1 to 10
  If i = 5 Then
    Exit For
  End If
  WScript.Echo i
Next


And here is an example of how to use Err.Raise to generate a runtime error:

1
2
On Error Resume Next
Err.Raise 9999, "MyScript", "An error occurred"


Member

by scotty , 4 months ago

@isadore 

Please note that using Err.Raise to stop execution should be used sparingly and carefully, as it can potentially lead to unexpected behavior or difficulties in debugging. It is generally recommended to use WScript.Quit or Exit statements to stop execution in a more controlled manner.