I'm trying the following code:
Try ' DOESN'T WORK Throw 2 ' How do I throw an exception? Catch ex 'What do I do here? End Try
but I'm getting the error Statement expected
in the catch clause.
Does anyone know how I can catch/throw exceptions in VBScript using try/catch? (I am not looking for solutions with On Error Do X
.)
VBScript doesn't have Try/Catch. (VBScript language reference. If it had Try, it would be listed in the Statements section.) On Error Resume Next is the only error handling in VBScript.
The Err object is part of the VBScript language and contains information about the last error to occur. By checking the properties of the Err object after a particular piece of code has executed, you can determine whether an error has occurred and, if so, which one.
catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed. The code in the finally block will always be executed before control flow exits the entire construct.
A sort of an "older style" of error handling is available to us in VBScript, that does make use of On Error Resume Next
. First we enable that (often at the top of a file; but you may use it in place of the first Err.Clear
below for their combined effect), then before running our possibly-error-generating code, clear any errors that have already occurred, run the possibly-error-generating code, and then explicitly check for errors:
On Error Resume Next ' ... ' Other Code Here (that may have raised an Error) ' ... Err.Clear ' Clear any possible Error that previous code raised Set myObj = CreateObject("SomeKindOfClassThatDoesNotExist") If Err.Number <> 0 Then WScript.Echo "Error: " & Err.Number WScript.Echo "Error (Hex): " & Hex(Err.Number) WScript.Echo "Source: " & Err.Source WScript.Echo "Description: " & Err.Description Err.Clear ' Clear the Error End If On Error Goto 0 ' Don't resume on Error WScript.Echo "This text will always print."
Above, we're just printing out the error if it occurred. If the error was fatal to the script, you could replace the second Err.clear
with WScript.Quit(Err.Number)
.
Also note the On Error Goto 0
which turns off resuming execution at the next statement when an error occurs.
If you want to test behavior for when the Set
succeeds, go ahead and comment that line out, or create an object that will succeed, such as vbscript.regexp
.
The On Error
directive only affects the current running scope (current Sub
or Function
) and does not affect calling or called scopes.
If you want to check some sort of state and then raise an error to be handled by code that calls your function, you would use Err.Raise
. Err.Raise
takes up to five arguments, Number
, Source
, Description
, HelpFile
, and HelpContext
. Using help files and contexts is beyond the scope of this text. Number
is an error number you choose, Source
is the name of your application/class/object/property that is raising the error, and Description
is a short description of the error that occurred.
If MyValue <> 42 Then Err.Raise(42, "HitchhikerMatrix", "There is no spoon!") End If
You could then handle the raised error as discussed above.
Change Log
Err.Clear
before the possibly error causing line to clear any previous errors that may have been ignored. On Error Resume Next
and Err.Clear
. Fixed some grammar to be less awkward. Added info on Err.Raise
. Formatting. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With