Re: Is David Fenton right about error handling?
- From: "Anthony England" <aengland@xxxxxxxxxx>
- Date: Wed, 4 Jan 2006 15:59:15 +0000 (UTC)
"Terry Kreft" <terry.kreft@xxxxxxxxx> wrote in message
news:3VadnWKUc8E_UCbeSa8jmw@xxxxxxxxxxxxxx
>
> Because you should be doing something like:-
>
> Exit_Handler:
>
> If Not rst Is Nothing Then
> if rst.state <> adStateClosed then
> rst.Close
> end if
> Set rst = Nothing
> End If
>
> Exit Function
>
> Err_Handler:
> MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
> Resume Exit_Handler
>
> End Function
>
> If you're in the clean up phase of your code, as you are in the
> Exit_Handler, then you need to ensure your code is solid. Personally I
> would code it as follows.
>
> Exit_Handler:
> On Error resume Next
> If Not rst Is Nothing Then
> if rst.state <> adStateClosed then
> rst.Close
> end if
> Set rst = Nothing
> End If
> On Error GoTo 0
> Exit Function
>
> Err_Handler:
> MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
> Resume Exit_Handler
>
> End Function
>
> On Error Resume Next can get you into infinite loops though, erm trying to
> think of an example, Oh yeah this should do it:-
>
> On Error resume Next
> Do While (rst.state and adStateOpen) = adStateOpen
> rst.Close
> Loop
> Set rst = Nothing
>
>
>
> --
> Terry Kreft
Hi Terry
Thanks for the input. I did realise what was causing the error and how the
code to avoid it. The purpose was simply to demonstrate the danger of an
error in the 'clean up and exit' part.
I like your version of the exit procedure - the general policy of which
seems to be:
"Try your very best to avoid any errors in the exit procedure (eg by
checking Is Nothing and rst.State) but realise that there is nothing much to
be done in the event of any unanticipated error and the key thing is to
reliably exit the function. To make double sure of this, put On Error
Resume Next at the beginning and explicitly turn it off at the end with On
Error GoTo 0."
A different policy might be:
"Code in the exit procedure should check for any anticipated eventuality
(such as rst not being initialized or opened) and I am sure that I cannot
predict an error. Therefore, I will use this version of the code and if an
error does occur in testing it will make itself known to me (albeit in an
annoying loop) and I can find out what is happening and I will become
marginally wiser and able to code for this error in the future.
Therefore in my code, I remove the extra two lines of code in the exit
handler:
Exit_Handler:
If Not rst Is Nothing Then
if rst.state <> adStateClosed then
rst.Close
end if
Set rst = Nothing
End If
Exit Function
Another take might be:
"I am so certain that an error cannot occur that I will let an unhandled
runtime error be generated if this does happen - at least I know I'll never
get stuck in a loop"
Exit_Handler:
On Error GoTo 0
If Not rst Is Nothing Then
if rst.state <> adStateClosed then
rst.Close
end if
Set rst = Nothing
End If
Exit Function
After considering these two alternatives, would you still prefer yours? (My
boss says he doesn't f***ing care, he just wishes I'd make a decision one
way or the other and get on with the coding)
.
- Follow-Ups:
- Re: Is David Fenton right about error handling?
- From: David W. Fenton
- Re: Is David Fenton right about error handling?
- From: Terry Kreft
- Re: Is David Fenton right about error handling?
- References:
- Is David Fenton right about error handling?
- From: Anthony England
- Re: Is David Fenton right about error handling?
- From: Bri
- Re: Is David Fenton right about error handling?
- From: Anthony England
- Re: Is David Fenton right about error handling?
- From: Terry Kreft
- Is David Fenton right about error handling?
- Prev by Date: Re: how to update new password to existing table in a custom login form using macros or query?
- Next by Date: Re: calling C# .Net dll from VBA
- Previous by thread: Re: Is David Fenton right about error handling?
- Next by thread: Re: Is David Fenton right about error handling?
- Index(es):
Relevant Pages
|