Error Handling in VBA
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 people to just ignore everything. So you end up with code like
On Error Resume Next
[Lot of junk]
Today I stumbled 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