Rubberduck icon indicating copy to clipboard operation
Rubberduck copied to clipboard

"OnErroGoto -1" warning has a wrong description

Open interservices opened this issue 2 years ago • 0 comments

Version 2.5.2.5906 OS: Microsoft Windows NT 10.0.22000.0, x64 Host Product: Microsoft Office x64 Host Version: 16.0.15225.20288 Host Executable: EXCEL.EXE


I am a (convinced) user of the (undocumented) instruction "On Error Goto -1" when I want complex inline error handlers, like this:

Sub example()
    Dim l&
    On Error GoTo errInLineHandler
    l = 5 / 0
errInLineHandler:
    If Err.Number <> 0 Then
         ' some complex code that could result in other errors
        On Error GoTo -1
    End If
    On Error GoTo 0
    
    'other code:
    l = 6 / 0
End Sub

The warning "OnErroGoto -1" that RD throws states that "the error state is also cleared in addition to disabling any error handling.

This description is wrong, as "On Error GoTo -1" only disactivate the error state, without disabling the enabled error handling ("On Error GoTo xxxxx").

Comment the line "On Error GoTo 0" in my code and you'll see that the following "l = 6 / 0" will jump to errInLineHandler instead than breaking the code as for the description meaning.

Furthermore, the description states that the On Error GoTo -1 "can be ambiguous as a negative line label of -1 may end up as a target [...]" while a negative labels cannot exist as the VBE immediately consider it a syntax error.

interservices avatar Jun 28 '22 11:06 interservices