Try..Catch..Finally in ... VBScript? Sure!
Saturday, 21. April 2007, 17:01:32
I've been working a lot with VBScript and Windows Scripting Host (WSH) lately. Unfortunately, as many of you VBScript writers know, the error-handling capabilities of VBScript leave a lot to be desired. Luckily, a simple design coding pattern from modern programming languages has revealed a rather unique technique in VBScript that resembles...well, a Try..Catch..Finally block! Yep, you heard correctly. With a liberal dose of multi-line colons ( : ), your VBScript code can attain near perfect Try..Catch..Finally functionality. Note: Guaranteed error-handling, and guaranteed finally block execution in a pseudo-subroutine fashion -- all without requiring an "On Error" statement!
Too good to be true? Here's the catch (no pun intended): You leverage the object lifespan provided by VBScript objects. VBScript supports OOP? Yes sir! Grab a copy of your favorite VBScript documentation, and do a search for "Class".
Basically, here's some pseudo-code that illustrates what we all wish VBScript supported...(yeah, I know VB.NET supports this, bah!)
And here's a reality check...
See a resemblance between the two examples? Sure. And guess what? While the first example doesn't work in VBScript, the second example runs just fine in VBScript 5.6 -- once the DoLog subrountine is defined
So explain, how does it work? Simple, really. You are defining a Class rather than a Sub-routine. Instantiated Classes (called objects) have a predefined lifespan: a constructor is guaranteed to run once the object is created; and a destructor is guaranteed to run once the object is destroyed. You simply leverage these innate object abilities to handle errors gracefully -- all without using "On Error" statements!
I hope you find this exercise fun, if not useful. I've been rewriting some larger WSH VBScripts using this new technique, and so far so good. I've thrown some various crash scenerios (from the past) at them, and not only did they manage to gracefully handle the errors, but the scripts completely finished without dying mid-way through them -- like they used to.
Enjoy.
Too good to be true? Here's the catch (no pun intended): You leverage the object lifespan provided by VBScript objects. VBScript supports OOP? Yes sir! Grab a copy of your favorite VBScript documentation, and do a search for "Class".
Basically, here's some pseudo-code that illustrates what we all wish VBScript supported...(yeah, I know VB.NET supports this, bah!)
Sub Func1
Try
DoLog "Starting"
Dim i : i = 65535 ^ 65535
MsgBox "Should not see this"
Catch e
Select Case e.Number
Case 6 DoLog "Overflow handled!"
Case Else DoLog "Unhandled error " & e.Number & " occurred."
End Select
Finally
DoLog "Exiting"
End Try
End Sub
Call Func1
And here's a reality check...
Class CFunc1
Private Sub Class_Initialize
DoLog "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 DoLog "Overflow handled!"
Case Else DoLog "Unhandled error " & Err.Number & " occurred."
End Select
Err.Clear : End Sub : Private Sub Class_Terminate : CatchErr
DoLog "Exiting"
End Sub
End Class
Dim Func1 : Set Func1 = New CFunc1 : Set Func1 = Nothing
See a resemblance between the two examples? Sure. And guess what? While the first example doesn't work in VBScript, the second example runs just fine in VBScript 5.6 -- once the DoLog subrountine is defined
So explain, how does it work? Simple, really. You are defining a Class rather than a Sub-routine. Instantiated Classes (called objects) have a predefined lifespan: a constructor is guaranteed to run once the object is created; and a destructor is guaranteed to run once the object is destroyed. You simply leverage these innate object abilities to handle errors gracefully -- all without using "On Error" statements!
I hope you find this exercise fun, if not useful. I've been rewriting some larger WSH VBScripts using this new technique, and so far so good. I've thrown some various crash scenerios (from the past) at them, and not only did they manage to gracefully handle the errors, but the scripts completely finished without dying mid-way through them -- like they used to.
Enjoy.

Can you give me an example of how you would use this in your WSH script. I understand how it works but I was wondering how you would embed it in your script. I'm also looking to use it in ASP. Most of my development is in Classic ASP. We haven't been Dotted yet.
Thanks,
Len
By LenChaney, # 23. August 2007, 13:57:05
Note: In this case, I used a global error-handling subroutine for the classes.
By Lee_Harvey, # 23. August 2007, 19:37:37