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

bobobobo
  • 64,917
  • 62
  • 258
  • 363
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 4
    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**. – Cody Gray - on strike Feb 15 '11 at 08:35
  • See below for some tips on using it correctly! :-) – Mark Sep 08 '11 at 05:30

4 Answers4

80

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.
  • Mark
    • 1,639
    • 1
    • 15
    • 20
    • 1
      I prefer "If Err" instead of "If Err.Number <> 0", but that's mostly a style preference. – Tmdean Apr 15 '15 at 17:32
    • 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. – Mark Apr 15 '15 at 19:03
    • 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. – user692942 Oct 21 '15 at 09:33
    • @Lankymart Thanks. I have clarified why I had the first `Err.Clear`. I have added information about `Err.Raise`. – Mark Oct 24 '15 at 05:15
    • 1
      Makes sense, thanks for the clarity. On the topic of Raising errors you might find [this article](http://www.4guysfromrolla.com/webtech/021201-1.shtml) interesting – user692942 Oct 24 '15 at 07:38
    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.

    Tmdean
    • 9,108
    • 43
    • 51
    • If it doesn't have Try/Catch, then how come it doesn't error on Try? – user541686 Feb 15 '11 at 03:54
    • 7
      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. – Tmdean Feb 15 '11 at 04:08
    • 3
      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. – AutomatedChaos Feb 15 '11 at 06:44
    • 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... :\ ) – user541686 Feb 15 '11 at 08:42
    • 4
      @Tmdean I actually googled Trraoysfoaiuerlkj. Thanks Genius. – You_Shall_Not_Pass Jul 29 '15 at 04:27
    • Under WSH, VBScript's event handling mechanism is a little simpler than in Javascript. See [here](https://msdn.microsoft.com/en-us/library/ms974564.aspx). – Zev Spitz Aug 23 '15 at 11:05
    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
    
    BoffinBrain
    • 6,337
    • 6
    • 33
    • 59
    • 8
      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. – Mark Aug 11 '13 at 01:22
    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.

    BoffinBrain
    • 6,337
    • 6
    • 33
    • 59
    Vital
    • 91
    • 1
    • 6
    • 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). – Mark Oct 30 '14 at 18:31