Simple code but error trap fails?

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Simple code but error trap fails?

Post by rory »

My 2p is still that if you are using On Error Goto -1, you should be rewriting, and probably refactoring, your code.
Regards,
Rory

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Simple code but error trap fails?

Post by Doc.AElstein »

rory wrote:
01 Feb 2021, 16:37
if you are using On Error Goto -1, you should be rewriting, and probably refactoring, your code.
I am not so clued up about the pros and cons of On Error GoTo -1 or I forgot..
But I am just a bit continually surprised that a lot of people trip up because this little bit of error stuff knowledge seems to have evaded them.
Keeping it a secret is, I expect, part of a larger major conspiracy - its part of the same conspiracy of Corona, ripping up all the beautiful old train tracks back in England in the 1960’s , the fake that VBA is, and other things to undermine the fundamental democratic rights and beautiful things that our fore fathers fought so hard for.
Never mind. Its all being noted, and we know who will be first against the wall when the revolution comes….
_._________________________________________________________________________________________________________


Coming back to the On Error Resume Next what’s it doing issue…

I still can’t quite figure it out, but had a few thoughts along the way.
We discussed the Err thing a while back, in particular what happens when you use it on its own , https://eileenslounge.com/viewtopic.php ... 49#p247149 . We concluded that Err is a function that returns an ErrObject , ( the default property being its number )

So you can use Err instead of Err.Number if you like, for example in an error handling code section where you might want to print out the error details of number and description, like
Debug.Print Err & vbCr & vbLf & Err.Description

Recently I noticed that you can also do this to get the same results
Debug.Print Err & vbCr & vbLf & Error
or
Debug.Print Err & vbCr & vbLf & Error()

I have tried to figure out what Error is
I still can’t make head nor tail of what the F2 help Object Explorer thing is all about and clicking randomly around after a search for Error tells me nothing substantial other than Error is a function maybe something to do with some Conversion thing…

I looked in a few other places, and picked up a bit of info ….

Error(x) returns you the error description of the error number x
Some Microsoft documentation suggests it should be blue, but it comes out black in my VB Editor coding, another conspiracy….

The one thing I gleaned form the documentation of interest is that it gets filled with text of the last error , and/or if you use it without a number argument, then that’s what it gives you.
So it looks to me as if Error and Err could in reality be very similar. One inspired guess is that there was a Error thing that got developed and along the way somebody made a typo and it turned into Err. By the time they noticed, it was too late to correct easily so they just carried on and glossed over the mistake.


So some further thoughts ( in the next post ) on On Error Resume Next …….._
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

On Error Resume Next

Post by Doc.AElstein »

_... continued from last post ..So some further thoughts on On Error Resume Next

I am not quite there yet. But here are some ideas that might help me get there…
The documentation and Blog sites that followed them over emphasise the On Error statement in my opinion.
Its not so special on its own. I am thinking there is something more fundamental, in that we have something like

On [ When this thing “is” ] Then [ Do this either when that “becomes” or if it already “is” when this code line is done ]

When this thing is set up there will be some data validation requirements ( leading to the syntax that is accepted ) and there will be a list of characters accepted in [ Do this either when that “becomes” or if it already “is” when this code line is done ] along with the actions that will be taken based on each of those words.
Those character string words wired into [ Do this either when that “becomes” or if it already “is” when this code line is done ] are just text strings chosen by someone, but they have tried to make them make some sense or relation to what’s happening. …
[ Do this either when that “becomes” or if it already “is” when this code line is done ] GoTo label is obvious from what goes on
[ Do this either when that “becomes” or if it already “is” when this code line is done ] Goto 0 or -1 we already discussed can sound meaningful for what actually happens
[ Do this either when that “becomes” or if it already “is” when this code line is done ] Resume Next is also meaningful since it does something which has the same effect of Resume Next , but just without the bit about clearing the Err or the Error


I think the above is another vote for what my definition / explanation of On Error Resume Next was
But it’s not conclusive yet. I think I will have this one figured in about 2-3 years time.

Just to refresh of my conclusion / Thoughts so far:

On Error Resume Next
If you use this user defined error hander then either the raised exception is removed/ reset pretty quickly after it is raised, or it is prevented from being raised at all. This has the effect something similar to a Resume Next, hence somebody one day decide to use those two words in the full code line ,
On [ When this thing “is” ] Then [ Do this either when that “becomes” or if it already “is” when this code line is done ]
On Error Resume Next

But it’s not quite the same as Resume Next since it does not clear the Err or the Error**
So the result of using this error handler is that the coding seems to continue as if no error had occurred , so it just carries on. **Whether by design or accident, it was not wired to clear Err or the Error and so we can use either of those to tell us about any error type that may have occurred. (It will tell us about the last error that occurred)

Alan

_.______________________________-


Edit P.S.1 I may not have made it clear about the first term in my suggested “model” of the On ___ ___ Statement
For the case on On Error Resume Next, the first bit ([ When this thing “is” ] ) will be Error. ( And it will become Blue in this situation )

P.S 1b) A Passing observation: There are likely other things wired into this On ___ ___ Statement. Other little secrets waiting to be discovered

Example: if I have
Goto Label in the second bit, ( [ Do this either when that “becomes” or if it already “is” when this code line is done ] )
then anything evaluating to a number, or capable of being coerced into an integer number from 0 to 255 will be accepted in the first part
[ When this thing “is” ]
The action taken will be
_ to got to the spring point if first part evaluates to 1 ( .500001 – 1.499999 )
_ ignore any other number in the allowed range ( -0.5 - 255.49999 )
Example macro here:
https://excelfox.com/forum/showthread.p ... #post15394

This last example may also be a contributing reason for someone choosing the words or charactes of
"GoTo -1 "
and
" GoTo 0 "
for the On Error _____, since these unlikely spring points help not to get confused with some valid spring point
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also