Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you ever use "On Error Goto 0"?

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?

like image 876
CJ7 Avatar asked Apr 02 '12 20:04

CJ7


People also ask

What is On error GoTo?

On Error GoTo lineEnables the error-handling routine that starts at line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to line, making the error handler active.

Where do you put error GoTo?

So you write your own error handling code and use On Error GoTo [LABEL] to instruct VBA to use your code to deal with errors. You can place your error-handling code anywhere in a procedure, but typically it is placed at the end.

How do I stop a GoTo error?

To shut off (disable) the active handler, use On Error GoTo 0 . Doing so will close off the code block that uses that handler. Alternatively, exit the subroutine using Exit Sub , which automatically turns off the handler.


2 Answers

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.

like image 181
Matt Dillard Avatar answered Sep 21 '22 12:09

Matt Dillard


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.

like image 25
Bob77 Avatar answered Sep 19 '22 12:09

Bob77