Some extra notes in support of this main forum Post
https://www.excelforum.com/excel-pro...ml#post5803728
http://www.eileenslounge.com/viewtopic.php?f=30&t=39437
The __Resumes versus the error handler On Error Resume Next
__On Error GoTo -1
Some re thoughts around March 2023
Slightly compacter way to do simple error handling in VBA
The threads referenced above got me into one of my annual re thinks about these things.
__Resumes versus the error handler On Error Resume Next
I already made the point in this Thread that we have two different things,
_ the 3 Resumes ( Resume and Resume Next and Resume LabelOrLineNumber )
, and the perhaps confusingly* named
_ On Error Resume Next
*The second thing is perhaps confusingly named, since the similarities with one of the Resumes are not as much as one might think.
In previous posts we did detailed pseudo codes. Here we will just summarise them, ( and add a link to the full pseudo codings ).
What is a new idea specific to what I am saying here is like… how about this, just a suggestion: The first thing that happens when an error occurs is not an immediate Exception State. Instead, the coding pauses. The Err object gets filled. Possibly the Erl() , is a more fundamental original Visual Basic thing, and is first given the information about the line that was last executed, the erroring code line. But the important new suggestion here is that the macro is paused and information about the error is registered. This will be _(i) in the pseudo codings below,
Then a decision is made
**Before considering first the Resumes ( Resume and Resume Next and Resume LabelOrLineNumber ) , we must remind ourselves that they only work in the exception State. They only come into play, that is to say, have a possible use, after a On Error GoTo LabelOrLineNumber.
_ The 3 Resumes
Resume ( https://excelfox.com/forum/showthrea...ll=1#post10556 )
_(i) An error occurs. (Assume we have a On Error GoTo LabelOrLineNumber ). The macro is paused. Information relating to the error is registered/ registers get filled in. The macro continues at the place specified by LabelOrLineNumber. Code lines are carried out mostly normally , but we are in the Exception State
The decision on what to do for if a Resume is encountered will be:
_(ii) On Error GoTo -1 ' ( This will clear Err and Erl() and takes us out of the exception State, and so back to normal code running.)
_(iii) we continue to run the coding at the same line that caused the error
Resume Next ( https://excelfox.com/forum/showthrea...ll=1#post10557 )
_(i) An error occurs. (Assume we have a On Error GoTo LabelOrLineNumber ). The macro is paused. Information relating to the error is registered/ registers get filled in. The macro continues at the place specified by LabelOrLineNumber. Code lines are carried out mostly normally , but we are in the Exception State
The decision on what to do for if a Resume is encountered will be:
_(ii) On Error GoTo -1 ' ( This will clear Err and Erl() and takes us out of the exception State, and so back to normal code running.)
_(iii) we continue to run the coding at the line after the line that caused the error
Resume LabelOrLineNumber ( https://excelfox.com/forum/showthrea...ll=1#post10558 )
_(i) An error occurs. (Assume we have a On Error GoTo LabelOrLineNumber ). The macro is paused. Information relating to the error is registered/ registers get filled in. The macro continues at the place specified by LabelOrLineNumber. Code lines are carried out mostly normally , but we are in the Exception State
The decision on what to do for if a Resume is encountered will be:
_(ii) On Error GoTo -1 ' ( This will clear Err and Erl() and takes us out of the exception State, and so back to normal code running.)
_(iii) we continue to run the coding at the line specified
So That’s the 3 resumes out of the way. They are very similar, differing only on where the coding finally continues in the normal state. There is nothing new there. I have been preaching that for many years. Others have also, all be it, often a bit patchy, rarely complete guide.
_ On Error Resume Next Some new thoughts
Previously I considered pseudo coding using the On Error GoTo -1 at some point. Just now I am wondering about that. Perhaps the exception state is never reached. The following is just a new suggestion:
( We assume that an On Error Resume Next has been passed somewhere )
_(i) An error occurs. The macro is paused. Information relating to the error is registered.
_(ii) we continue at the next code line after the one that caused the error.
The slight difference in this new suggestion is that we never go into an exception state, or if we do it is somehow very quickly removed, but not in the same way as any of the Resumes are….
Another suggestion:
Just an idea.
_ On Error Resume Next :- this means we never go into The State of Exception. For no particular reason Microsoft limit us to carry on at the line after that erroring. One might wonder why they did not allow us to do something like ____On Error Resume .. at some other place of your convenience..
_ On Error GoTo LabelOrLineNumber:- this means we go into exception (when an error occurs). We can choose where we want a copy of the macro to continue (all be it in the State of Exception )
Some new neat ideas arising from the referenced thread, Slightly compacter way to do simple error handling in VBA
http://www.eileenslounge.com/viewtop...305642#p305642




Reply With Quote
Bookmarks