Error Handling in VBA

20.09.2007 at 14:52

Ok at work i have to deal with some ugly VBA code, and well as you probably can imagine this is not exactly fun. One thing which i find hilarious about VBA is it's approach to error handling which allows peoples to just ignore everything. So you end up with code like

On Error Resume Next
  [Lot of junk]

So and today i struggled over the following construct:

On Error GoTo 0

What the hell is 0 in this context? For sure it isn't a valid label. A quick google search revealed the above url which states that this restores normal error handling mode (that is a standard run time error message box will be displayed).

So with this you can kind of emulate try/catch/finally of a sane language.

On Error GoTo Catch
    [Do something which might cause an error]
    GoTo Finally
Catch:
    [An error occurred, do something]
Finally:
    On Error Goto 0 ' restore normal error handling

Marc

Comments (0)

There are currently no comments available