Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Clearing errors

I am having trouble with sending my errors where I want them to go.

I have code that basicly is as such.

sub1()
On error goto Error1
Call sub2
--
exit sub
Error1:--
end sub

Sub2()
on error goto Error2
-- this line of code generates an error and the program goes to Error2
--
exit sub
Error2: Err.Clear
On error goto Error3
-- This line of code also generates an error but in place of going to Error3 it goes to Error1
exit sub
Error3: Err.Clear
--
end sub
[593 byte] By [AM003295] at [2007-11-11 9:58:24]
# 1 Re: Clearing errors
Vb accept only one "On error" statment per sub or function .. so in your "Sub2()" :
the first declared "On error" statment tell the programme to go to label "Error2" ..

the second one which seems must go to error3 is in real ignored so your programme work as this :

-know that in sub1 if error occure it must go to error1 label .
-call sub2 .
-know that in sub2 if error occure it must go to error2 label [and the second is ignored] .
-error ocure .
-goto error2 label .
-another error ocure .
-the programme is interupted and the sub2 make an error .
-here remember that sub2 run as function in sub1 so sub1 recive an error and raise it's error call .

To show this more clearly remove "On error goto Error1" from sub1 .
Amahdy at 2007-11-11 17:23:31 >
# 2 Re: Clearing errors
You are structuring your subroutine incorrectly. You need to think in terms of one error trap that decides what the correct action should be. Something more like this:
Sub2()
On Error Goto Er2
... code ...
cnt1:
... more code ...
cnt2:
... even more code ...

Done:
On Error Resume Next
... clean up code here ...
Exit Sub

Er2:
Select Case Err.Number
Case softerror1
'Ok to continue at point 1
Resume cnt1
Case softerror2
'Ok to continue at point 2
Resume cnt2
End Select
'Hard error so display message and resume as cleanup code
MsgBox Err.Number & " -- " & Err.Description
Resume Done
End Sub
Ron Weller at 2007-11-11 17:24:31 >
# 3 Re: Clearing errors
Ron is right
Once that VB catches an error, all subsequential calls to "On error" in the same mehod are ignored (so you can have more than one "On Error" statements, but you cannot put them in a section of the code that handles an error...)

VB handling of errors is indeed not very flexible. What I do is trying to make methods VERY short, and to nest them one inside the other, with one error handling on each of them, so it is easier to catch the error and to know where it comes from. Of course, having nesting calls, there is a need for the "caller" to know if the mehod was ok or not, to do that I makes the method as functions, and I check the return values. If it is not what is expected to be, I just exit the current method, something like

if myFuntion1(...) then
''' eventually clean up here
exit sub
endif

where myFunction1 returns a boolean if ok

Of course, this is just another example on how to handle errors in VB. Sometimes I use the same method as Ron just suggested
Because, as I said before, VB error handling is not that great, almost every decent VB manual and/or book has a section that explains the best way to use it
mstraf at 2007-11-11 17:25:29 >
# 4 Re: Clearing errors
"Vb accept only one "On error" statment per sub or function "

Don't believe this is the case ;-) - you might be expecting an error on a particular 'function' - e.g. open a file and it is not there, in which case you may have 'on error goto 'wherever', but having successfully opened the file, any subsequent error may be sent to a different 'handler' with a different 'on error goto ...'
gupex at 2007-11-11 17:26:35 >
# 5 Re: Clearing errors
If you look at his code you will see that he never used the resume statement, this is why VB seemed to be ignoring his other on error statements. Based on his symptoms VB will not trigger another error while it thinks you are already in the error trapping code. The only way to clear this is either using the resume or exiting the sub.
Ron Weller at 2007-11-11 17:27:31 >
# 6 Re: Clearing errors
Yea Ron is right, I meant if he use :
On Error goto lbl1
AND
On Error goto lbl2
only the first one will work , whatever the code is .

I haven't well formed the statment , it must be (only one "On error goto xxx") .
Amahdy at 2007-11-11 17:28:30 >