Solved On Error Resume Next : Exit Sub... why? (1 Viewer)

Notiophilus

Registered User.
Local time
Today, 11:16
Joined
Jan 18, 2015
Messages
42
Going through my spaghetti code I find the following error handler:
Code:
ExitHandler:
    On Error Resume Next
    Exit Sub

ErrHandler:
    Call LogError
    Resume Next

End Sub

What's the purpose of "Resume Next" right before exiting? Can Exit Sub throw an error?
I see this all over the place, but I can't find an explanation though there's plenty of examples of its use, e.g.: https://www.devhut.net/ms-access-vba-error-handling/
 

GPGeorge

Grover Park George
Local time
Today, 03:16
Joined
Nov 25, 2004
Messages
1,776
Going through my spaghetti code I find the following error handler:
Code:
ExitHandler:
    On Error Resume Next
    Exit Sub

ErrHandler:
    Call LogError
    Resume Next

End Sub

What's the purpose of "Resume Next" right before exiting? Can Exit Sub throw an error?
I see this all over the place, but I can't find an explanation though there's plenty of examples of its use, e.g.: https://www.devhut.net/ms-access-vba-error-handling/
That is a trick Access developers use to avoid "bonus" errors in the ExitHandler.

In some cases, the ExitHandler will have additional lines of code between the On Error Resume Next and the Exit Sub lines; things like closing any open recordsets for example. If the attempt to close a recordset IN THAT ExitHandler portion throws another error because it was already closed elsewhere, you don't want to blow up the whole thing; just ignore it and proceed to the Exit Sub. That's what On Error Resume Next does for you.
It is, perhaps, superfluous here but many of us have "boiler plate" error handling that gets dropped in by tools like MZTools or VTools, etc.
 

Notiophilus

Registered User.
Local time
Today, 11:16
Joined
Jan 18, 2015
Messages
42
Ah, so it's just a leftover of normal good practice. Makes sense, thanks!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:16
Joined
Feb 28, 2001
Messages
27,001
Have to agree with GPGeorge that it is normally just a precaution if there is more exit code than just an Exit Sub. Let me 'splain what that REALLY does. In the specific example you gave us, it was like wearing a belt and suspenders at the same time. (That is USA "suspenders", in UK "braces.")

When you are in a routine, you have a stack frame built by the Call that entered that routine. (Yes, your Event routines are CALLED by Access.) Events are not interrupts. They are just things that Access recognizes and declares at certain times.

Part of that call stack for that subroutine has been set aside for various things - actual call parameters (if any), local variables (if any), the address of the caller, AND the declared start of the current Trap Handler. An ON ERROR GOTO ErrorTrap loads the address of ErrorTrap to that trap handler slot. An ON ERROR GOTO 0 cancels (loads Nothing/Null) to the trap handler slot. I believe (but cannot prove) that ON ERROR RESUME NEXT points to a "hidden" target that only has a RESUME NEXT statement. (It might also just set a flag... but since we can't see Access internal code, who knows?) The point is that you would use the RESUME NEXT at ANY time that you want to say "No matter what error you might encounter, keep on going through this code - silently." I have used it many times when dealing when testing whether specific application objects exist at the moment, and you get testable error conditions if they don't. Lots of other uses.

So in that sense, the ON ERROR RESUME NEXT simply blocks all error handlers. Since you are about to exit the subroutine and dissolve the stack frame, the current Trap Handler slot is about to go away anyway, to be replaced by your caller's stack frame.
 

Users who are viewing this thread

Top Bottom