Post #10 on page #1 https://excelfox.com/forum/showthrea...ll=1#post10558
Error Handling statement Resumes
On Error Resume Next
The next posts will look at the second main Error handling statement , On Error Resume Next .
Every time I look at this, I come up with a slightly different idea. To some extent I may be going back and forwards, fluctuating a bit, bit on average I am perfecting the understanding a bit better.
If I try, as I have done in the past, to discus the more fundamental general idea of the Resume .
And build on that, to derive user defined error handlers that works in a similar way, things don’t come outs quite as logically as one might expect. .
In my opinion the whole VBA error handling is an unintuitive mess. In the unlikely event that you have read and understood most of my previous ramblings, then a couple logical conclusion might be the following:
_ We might reasonably guess that there is no On Error Resume
This is because that would imply that at an error the code tries again. And again. And again And again. And again.. … Conceivable there might be a situation where that might work if some external influence meant that the code line erroring suddenly didn't. But that is unlikely. More likely is that having such an option would cause the attempt at an infinite loop or retrying.
In fact that assumption is correct. There is no On Error Resume
_ We might reasonably guess that there might be a On Error Resume LabelOrLineNumber
That would be a reasonable thing to do. But there isn’t. Perhaps this should slightly spark our suspicions.
Indeed we find the On Error Resume Next is not quite doing _ On Error ……… then …. Resume Next. Almost it does. It does both
_ carry on after the line causing the error
, and
_ it appears to not be in The State of Exception
But here is the oddity. The error object, Err , is not cleared and so can still be used to tell us what error did occur , (and the Erl() also tells us the last line number that errored )
On Error Resume Next is very commonly used ( badly in the opinion of most professionals) to just keep a coding going despite any errors
This simplest use is shown in the next macro. All errors are ignored.
Code:
Sub OnErrorResumeNext1() ' https://excelfox.com/forum/showthread.php/2239-Resume-On-Error-GoTo-0-1-GoTo-Error-Handling-Statements-Runtime-VBA-Err-Handling-ORNeRe-GoRoT-N0Nula-1?p=10559&viewfull=1#post10559
0 Dim Ex As Double
10 On Error Resume Next
20 Let Ex = 1 / 0
30 MsgBox prompt:=Err.Description & ", at line number" & Erl(): Debug.Print Err.Description & ", at line number" & Erl()
40 Let Ex = 1 / 0
50 MsgBox prompt:=Err.Description & ", at line number" & Erl(): Debug.Print Err.Description & ", at line number" & Erl()
End Sub
'
The output is like
Division durch Null, at line number20
Division durch Null, at line number40
Bookmarks