Why would you ever use "On Error Goto 0"
in a VB6 app?
This statement turns the error handler off and would mean that any error would crash the app. Why would this ever be desirable?
Why would you ever use "On Error Goto 0"
in a VB6 app?
This statement turns the error handler off and would mean that any error would crash the app. Why would this ever be desirable?
In VB6, you can specify that you want errors to be handled by particular code later in the routine:
Sub Bar()
On Error Goto MyHandler
...
...some code that throws an error...
...
Exit Sub
MyHandler:
...some error handler code (maybe pops up a dialog)
End Sub
It may be the case, however, that the code that throws the error is localized, and you don't want that same handler for all of the rest of the code in the routine. In that case, you'd use "On Error Goto 0" as follows:
Sub Bar()
...
On Error Goto MyHandler
...some code that throws an error...
On Error Goto 0
...
...
Exit Sub
MyHandler:
...some error handler code (maybe pops up a dialog)
End Sub
Now you have effectively scoped the error handling to execute only if that particular line of code fails.
By calling "On Error Goto 0" you are NOT saying that you want the app to crash immediately. You are simply saying that you want to de-register any error handlers that you may have set up earlier in the routine; errors will be passed up the call stack to calling routines, like normal.
Since it seems to be clumsy to describe in words, here are some examples showing where you can use On Error GoTo 0
for localized, structured error handling.
The first is a Property Get
in a class ("MicroDOM") that implements a lightweight DOM based on a hierarchy of subclassed Collections. In this case we want an attempt to reference a missing Child by name instead of index to create an empty (no attrbutes or children) Child:
Public Property Get Child(ByVal Key As Variant) As MicroDOM
If mChildren Is Nothing Then
Set mChildren = New Collection
End If
On Error Resume Next
Set Child = mChildren(Key)
If Err Then
On Error GoTo 0
If VarType(Key) = vbString Then
Key = Trim$(Key)
Set Child = New MicroDOM
Child.Key = Key
mChildren.Add Child, Key
Else
Err.Raise 9 'Subscript error as thrown by the Collection.
End If
End If
End Property
The second is inline code that deletes a file if it is present:
On Error Resume Next
Kill strFilePath
On Error GoTo 0
The third is inline code that takes an action only if a file happens to be present:
On Error Resume Next
GetAttr strFilePath
If Err Then
On Error GoTo 0
ProcessTheData strFilePath
End If
On Error GoTo 0
While it may appear awkward to the uninitiated (executing On Error GoTo 0
in two places) the result is less clumsy and more structured than having rafts of On Error GoTo Label
that jump back and forth to process various exceptions.
The bonus is that you gain portability to VBScript as well, since On Error GoTo Label
isn't a valid construct there at all.
It only turns off error handling in the CURRENT procedure. If there is an error handler in the calling procedure it will catch any exceptions that weren't handled. VB keeps going up the call stack until it finds an error handler. If it doesn't find any THEN it will cause the run-time error.
So for an example - maybe you have a wrapper function that calls some third-party utility that may throw an exception. Instead of handling the exceptions in the wrapper function you put a On Error Goto 0
in there. So then the caller of the wrapper function will then get the exception passed to it and hopefully handle it in a proper way.
You might find this link helpful: http://answers.microsoft.com/en-us/office/forum/office_2010-excel/why-on-error-resume-next-and-on-error-goto-0-have/a110548f-95c9-44ac-89bc-19697641804a?auth=1
Basically it explains that On Error Resume Next tells VB to skip any errors found and go to the next procedure or line in your code, while On Error GoTo 0 restores default error handling.