- Local time
- Today, 07:28
- Joined
- Feb 28, 2001
- Messages
- 28,808
Obviously, my intent wasn't clear. I was commenting on an omission that I felt left something unclear in the article referenced by @Nautical Gent in his post #47 of this thread.
@Pat Hartman - your suggested usage of Return (Next or a declared statement label) is absolutely correct. No issues there.
@isladogs - yes, true. Allen didn't use GoTo in his code - as well he should not have. There is a danger in using GOTO X in an error handler IF you make the mistake of using it as though it meant the same thing as RESUME X. That is because GOTO X doesn't clear the error context whereas RESUME X does.
@Isaac - Since Access is single-threaded, the only error that CAN occur at that point would be in the error routine itself. I will answer the question later about what happens when an error occurs in an error handler.
You can allow an exit handler to fall into an END SUB and it will be OK because Access tests for stacked context. However, you need to realize where you are when an error occurs and falls through to the END SUB. You have exited the subroutine when that happens. As for example, Pat's idea of using RESUME SUB_EXIT_POINT or whatever label floats your boat.
Suppose you have Sub A which declares a handler via On Error GoTo A_Handler.
In the course of coding Sub A, you call separately declared Sub B which does NOT declare a handler.
In the course of coding Sub B, you call separately declared Sub C which does NOT declare a handler.
Now it turns out that you have a condition that will trigger a run-time error in Sub C. Where will the trap occur?
The answer is A_Handler takes C's trap.
(Read the "Remarks" section relating to active vs. inactive error handlers.)
OK, Isaac's question: Let's say that above, Sub C actually DOES have an error handler, call it C_Handler, but it is faulty AND gets called due to an error in the "normal" part of Sub C. If an error handler gets activated and there is an error in the code,...
In the three layers I named above, C cannot take the trap because its error handler is already activated. B cannot take the trap because no handler was declared. So I believe in the case of Isaac's question, A_Handler would take that trap.
Finally, again suppose that Sub C gets called, trips over itself such that the error handler C_Handler gets called; but C_Handler has an error, too, and eventually the error "bubbles up" such that A_Handler takes the error. A_Handler finishes with a RESUME NEXT. In this case, which line of code is executed next?
Answer: The line following the call (in the context of Sub A) to Sub B.
Sub C and its instantiation vanished when C_Handler failed and the error got passed up to B - which ALSO failed because the error had to be passed back up another layer - to A_Handler. From A_Handler (which is part of Sub A) you cannot return to any label in B or C via a RESUME SOME_LABEL - because they are no longer in scope. The only thing in scope at that point is Sub A.
The next reference emphasizes the point that the "current" error handler doesn't necessarily have to be in the same routine in which the error occurred. It's an Excel source but the VBA advice is spot-on.
My point, I guess, was that I didn't think the article brought up as much as I thought it should. You guys know that sometimes I get a wild hair somewhere. This post resulted from scratching where that wild hair itched.
@Pat Hartman - your suggested usage of Return (Next or a declared statement label) is absolutely correct. No issues there.
@isladogs - yes, true. Allen didn't use GoTo in his code - as well he should not have. There is a danger in using GOTO X in an error handler IF you make the mistake of using it as though it meant the same thing as RESUME X. That is because GOTO X doesn't clear the error context whereas RESUME X does.
@Isaac - Since Access is single-threaded, the only error that CAN occur at that point would be in the error routine itself. I will answer the question later about what happens when an error occurs in an error handler.
You can allow an exit handler to fall into an END SUB and it will be OK because Access tests for stacked context. However, you need to realize where you are when an error occurs and falls through to the END SUB. You have exited the subroutine when that happens. As for example, Pat's idea of using RESUME SUB_EXIT_POINT or whatever label floats your boat.
Suppose you have Sub A which declares a handler via On Error GoTo A_Handler.
In the course of coding Sub A, you call separately declared Sub B which does NOT declare a handler.
In the course of coding Sub B, you call separately declared Sub C which does NOT declare a handler.
Now it turns out that you have a condition that will trigger a run-time error in Sub C. Where will the trap occur?
The answer is A_Handler takes C's trap.
(Read the "Remarks" section relating to active vs. inactive error handlers.)
OK, Isaac's question: Let's say that above, Sub C actually DOES have an error handler, call it C_Handler, but it is faulty AND gets called due to an error in the "normal" part of Sub C. If an error handler gets activated and there is an error in the code,...
In the three layers I named above, C cannot take the trap because its error handler is already activated. B cannot take the trap because no handler was declared. So I believe in the case of Isaac's question, A_Handler would take that trap.
Finally, again suppose that Sub C gets called, trips over itself such that the error handler C_Handler gets called; but C_Handler has an error, too, and eventually the error "bubbles up" such that A_Handler takes the error. A_Handler finishes with a RESUME NEXT. In this case, which line of code is executed next?
Answer: The line following the call (in the context of Sub A) to Sub B.
Sub C and its instantiation vanished when C_Handler failed and the error got passed up to B - which ALSO failed because the error had to be passed back up another layer - to A_Handler. From A_Handler (which is part of Sub A) you cannot return to any label in B or C via a RESUME SOME_LABEL - because they are no longer in scope. The only thing in scope at that point is Sub A.
The next reference emphasizes the point that the "current" error handler doesn't necessarily have to be in the same routine in which the error occurred. It's an Excel source but the VBA advice is spot-on.
VBA Error Handling - A Complete Guide - Excel Macro Mastery
“Abort, Retry, Fail?” – MS-DOS error message circa 1986 This post provides a complete guide to VBA Error Handing. If you are looking for a quick summary then check out the quick guide table in the first section. If you are looking for a particular topic on VBA Error Handing then check out the...
excelmacromastery.com
My point, I guess, was that I didn't think the article brought up as much as I thought it should. You guys know that sometimes I get a wild hair somewhere. This post resulted from scratching where that wild hair itched.