Error Handling construct
Thank you - the example made it clear and allowed me to experiment.
Geoff
"James Snell" wrote:
Yes, that's correct.
On Error GoTo 0 means for vba to handle errors (usually report them to the
user) which means in subproc2 (as there is no error handling there) and once
subproc2 returns vba will be handling errors and not your error handler.
"Geoff" wrote:
Ok so it must be the On Error Resume Next before the Find statement in Main
that seems to disable myErrorHandler. So I'm thinking I should restate the
handler at that point instead of the following On Error GoTo 0?
Geoff
"James Snell" wrote:
On Error is procedural, so changing it in a child routine does not affect the
parent.
Proof of the pudding is in the eating though... So here's a sample.
Sub TestErr()
On Error GoTo errHandle
Call NaughtyChild
Err.Raise 2
MsgBox "error handler not called - oops"
End
errHandle:
MsgBox "error handler called - you do not need to restate the on error"
End Sub
Sub NaughtyChild()
On Error Resume Next
Err.Raise 1
Err.Clear
End Sub
"Geoff" wrote:
Hi
From tests and 'Help' it seems I need to restate On Error GoTo
myErrorHandler after returning from each sub proc and after the On Error
Resume Next.
Is that correct or is there a better way to provide error handling
throughout Main?
T.I.A.
Geoff
Sub Main()
On Error GoTo myErrorHandler
blah
SubProc1 ''no error handler in this proc
On Error Resume Next
foundIt = Sheets(1).Find("xxx", , , xlWhole)
On Error GoTo 0
SubProc2 ''no error handler in this proc
blah
Exit Sub
myErrorHandler:
If Err.Number < 0 Then
'''do something
Err.Clear
End If
End Sub
|