Discussion:
Error handling problem
(too old to reply)
a***@b.com
2012-02-07 22:55:37 UTC
Permalink
In the following code the first time an error occurs in (code 2) the
error handler goes to Err_Label2.
But on the next loop iteration if an error occurs in block (code 2)
neither of the error handlers are called.
The error is thrown back to the caller that called MyFunction.
It is as if on the second iteration the error handler
"On Error Goto 0" is in effect.

Can anyone reproduce and/or explain this behavior?


Function MyFunction()

Dim ErrNum

On Error Goto Err_Label

(code 1 - read recordset MyRs)

MyRs.MoveFirst
Do Until MyRs.EOF

On Error Goto Err_Label2

(code 2)

Err_Label2:
ErrNum = Err.Number
Err.Clear

On Error Goto Err_Label

If ErrNum <> 0 Then
(do something)
Else
(do something else)
End If

MyRs.MoveNext

Loop

Exit_Label:
(cleanup code)

Exit Function

Err_Label:
(code)
Resume Exit_Label

End Function
Deanna Earley
2012-02-08 09:18:13 UTC
Permalink
Post by a***@b.com
In the following code the first time an error occurs in (code 2) the
error handler goes to Err_Label2.
But on the next loop iteration if an error occurs in block (code 2)
neither of the error handlers are called.
The error is thrown back to the caller that called MyFunction.
It is as if on the second iteration the error handler
"On Error Goto 0" is in effect.
Almost, Once you it hits an "On Error Goto XX" handler, then it is IN
that handler and you MUST "Resume XXX" to continue.
In your case, it jumps into the error handler on the first iteration.
On the 2nd iteration, it's already in that handler (regardless of where
in the procedure it is) and it jumps up to the error handler in the
calling code.
Post by a***@b.com
Function MyFunction()
Dim ErrNum
On Error Goto Err_Label
(code 1 - read recordset MyRs)
MyRs.MoveFirst
Do Until MyRs.EOF
On Error Goto Err_Label2
(code 2)
ErrNum = Err.Number
If ErrNum <>0 then
Post by a***@b.com
Err.Clear
On Error Goto Err_Label
If ErrNum<> 0 Then
(do something)
Else
(do something else)
End If
Resume Err_LabelCont
End If
Post by a***@b.com
MyRs.MoveNext
Loop
(cleanup code)
Exit Function
(code)
Resume Exit_Label
End Function
An alternative to the On Error, Resume model is to use On Error Resume
Next and explicitly check the value of Err.Number afterwards.
If you have a block of code, you will need to check after every call to
stop it trying to carry on until it gets to the error check.
--
Deanna Earley (***@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)
a***@b.com
2012-02-08 19:22:40 UTC
Permalink
Thanks for the reply.
Very helpful.

On Wed, 08 Feb 2012 09:18:13 +0000, Deanna Earley
Post by Deanna Earley
Post by a***@b.com
In the following code the first time an error occurs in (code 2) the
error handler goes to Err_Label2.
But on the next loop iteration if an error occurs in block (code 2)
neither of the error handlers are called.
The error is thrown back to the caller that called MyFunction.
It is as if on the second iteration the error handler
"On Error Goto 0" is in effect.
Almost, Once you it hits an "On Error Goto XX" handler, then it is IN
that handler and you MUST "Resume XXX" to continue.
In your case, it jumps into the error handler on the first iteration.
On the 2nd iteration, it's already in that handler (regardless of where
in the procedure it is) and it jumps up to the error handler in the
calling code.
Post by a***@b.com
Function MyFunction()
Dim ErrNum
On Error Goto Err_Label
(code 1 - read recordset MyRs)
MyRs.MoveFirst
Do Until MyRs.EOF
On Error Goto Err_Label2
(code 2)
ErrNum = Err.Number
If ErrNum <>0 then
Post by a***@b.com
Err.Clear
On Error Goto Err_Label
If ErrNum<> 0 Then
(do something)
Else
(do something else)
End If
Resume Err_LabelCont
End If
Post by a***@b.com
MyRs.MoveNext
Loop
(cleanup code)
Exit Function
(code)
Resume Exit_Label
End Function
An alternative to the On Error, Resume model is to use On Error Resume
Next and explicitly check the value of Err.Number afterwards.
If you have a block of code, you will need to check after every call to
stop it trying to carry on until it gets to the error check.
Loading...