Why would you ever use “On Error Goto 0”

error-handlingvb6

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?

Best Solution

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.