Try-Catch-End Try in VBScript doesn't seem to work
Asked Answered
P

4

43

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.)

Prevenient answered 15/2, 2011 at 2:25 Comment(2)
Then you're going to be very disappointed. There's no Try-Catch support in pre-.NET versions of VB, including VB 6, VBA, and VBScript. On Error Resume Next isn't so bad if you know how to use it correctly.As
See below for some tips on using it correctly! :-)Franciscafranciscan
O
24

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. Sorry. If you want try/catch, JScript is an option. It's supported everywhere that VBScript is and has the same capabilities.

Omen answered 15/2, 2011 at 3:30 Comment(6)
If it doesn't have Try/Catch, then how come it doesn't error on Try?Prevenient
I guess it must support Trraoysfoaiuerlkj too, becuase if you change the Try to that it doesn't error there either. ;) I guess it thinks Try might be the name of the Sub that comes later in the script.Omen
That is right, VBScript sees it as a Sub without arguments, so it doesn't error if you don't use Option Explicit or if you use On Error Resume Next. That is the reason why you should use Option Explicit regularly and On Error Resume Next sparcely. Because if you make an typo, you can easily detect it by the mention of an undeclared statement or unrecognized sub/function.Penelopepeneplain
Oh, I never knew that Option Explicit works in VBScript... I only used it in VB 6, thanks! (I also thought Try was a keyword because Notepad++ highlighted Try and Catch... :\ )Prevenient
@Omen I actually googled Trraoysfoaiuerlkj. Thanks Genius.Colonize
Under WSH, VBScript's event handling mechanism is a little simpler than in Javascript. See here.Giuseppinagiustina
F
81

Handling Errors

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.


Raising Errors

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

  • Edit #1: Added an Err.Clear before the possibly error causing line to clear any previous errors that may have been ignored.
  • Edit #2: Clarified.
  • Edit #3: Added comments in code block. Clarified that there was expected to be more code between On Error Resume Next and Err.Clear. Fixed some grammar to be less awkward. Added info on Err.Raise. Formatting.
  • Franciscafranciscan answered 5/5, 2011 at 22:29 Comment(5)
    I prefer "If Err" instead of "If Err.Number <> 0", but that's mostly a style preference.Omen
    I was gonna change it, but it feels a little weird to me to be calling Err.Clear and using a "if not null" style check after. All down to style preference.Franciscafranciscan
    The first Err.Clear is superfluous as On Error Resume Next resets the Err object for you. Also make no mention of Err.Raise a very useful tool in the VBScript Error Handling arsenal.Eno
    @Lankymart Thanks. I have clarified why I had the first Err.Clear. I have added information about Err.Raise.Franciscafranciscan
    Makes sense, thanks for the clarity. On the topic of Raising errors you might find this article interestingEno
    O
    24

    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. Sorry. If you want try/catch, JScript is an option. It's supported everywhere that VBScript is and has the same capabilities.

    Omen answered 15/2, 2011 at 3:30 Comment(6)
    If it doesn't have Try/Catch, then how come it doesn't error on Try?Prevenient
    I guess it must support Trraoysfoaiuerlkj too, becuase if you change the Try to that it doesn't error there either. ;) I guess it thinks Try might be the name of the Sub that comes later in the script.Omen
    That is right, VBScript sees it as a Sub without arguments, so it doesn't error if you don't use Option Explicit or if you use On Error Resume Next. That is the reason why you should use Option Explicit regularly and On Error Resume Next sparcely. Because if you make an typo, you can easily detect it by the mention of an undeclared statement or unrecognized sub/function.Penelopepeneplain
    Oh, I never knew that Option Explicit works in VBScript... I only used it in VB 6, thanks! (I also thought Try was a keyword because Notepad++ highlighted Try and Catch... :\ )Prevenient
    @Omen I actually googled Trraoysfoaiuerlkj. Thanks Genius.Colonize
    Under WSH, VBScript's event handling mechanism is a little simpler than in Javascript. See here.Giuseppinagiustina
    Z
    4

    Try Catch exists via workaround in VBScript:

    http://web.archive.org/web/20140221063207/http://my.opera.com/Lee_Harvey/blog/2007/04/21/try-catch-finally-in-vbscript-sure

    Class CFunc1
        Private Sub Class_Initialize
            WScript.Echo "Starting"
            Dim i : i = 65535 ^ 65535 
            MsgBox "Should not see this"
        End Sub
    
        Private Sub CatchErr
            If Err.Number = 0 Then Exit Sub
            Select Case Err.Number
                Case 6 WScript.Echo "Overflow handled!" 
                Case Else WScript.Echo "Unhandled error " & Err.Number & " occurred."
            End Select
            Err.Clear
        End Sub
    
        Private Sub Class_Terminate
            CatchErr
            WScript.Echo "Exiting" 
        End Sub 
    End Class
    
    Dim Func1 : Set Func1 = New CFunc1 : Set Func1 = Nothing
    
    Zhao answered 22/2, 2013 at 5:36 Comment(1)
    While this is kinda cool, in the sense that it's clever, "clever" code is rarely readable or easily maintainable/usable. I'm not comfortable doing a -1 on this, but this really is "bad code" for production purposes. Cool academically though.Franciscafranciscan
    M
    0

    Sometimes, especially when you work with VB, you can miss obvious solutions. Like I was doing last 2 days.

    the code, which generates error needs to be moved to a separate function. And in the beginning of the function you write On Error Resume Next. This is how an error can be "swallowed", without swallowing any other errors. Dividing code into small separate functions also improves readability, refactoring & makes it easier to add some new functionality.

    Melnick answered 28/10, 2013 at 9:22 Comment(1)
    Or you could put On Error Goto 0 after the code that generates your error, so that you don't incur the overhead of a function call (and don't need tiny functions that don't do any real work).Franciscafranciscan

    © 2022 - 2024 — McMap. All rights reserved.