Re: Is David Fenton right about error handling?



"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)


.



Relevant Pages

  • Re: Is David Fenton right about error handling?
    ... If Not rst Is Nothing Then ... On Error Goto 0 ... >> Exit Function ...
    (comp.databases.ms-access)
  • Re: Why is GOTO bad?
    ... have just put it in a function in those cases and used exit function. ... If you could specify which do loop you were exitting then ... It is basically constructs of a given language. ... and also preferably with BREAK (or EXIT out of loop) and CONTINUE supported. ...
    (microsoft.public.vb.general.discussion)
  • Re: how do people feel about exit function from loop
    ... How do people feel about using exit function from inside of a loop? ... For intCountElement = 1 To objConditionalFormats.Count ... And I wouldn't argue w/ an Exit For then an Exit, either, but I'd still write the simpler conditional on the loop for most purposes. ...
    (microsoft.public.vb.general.discussion)
  • Re: how do people feel about exit function from loop
    ... How do people feel about using exit function from inside of a loop? ... or exit function is considered bad because it is likened to Goto. ...
    (microsoft.public.vb.general.discussion)
  • Re: Exit Do
    ... how woud he break out of a loop with a conditional statement? ... not a great way to end a loop. ... I agree that Exit Do statements are fine. ... and Exit Function. ...
    (microsoft.public.scripting.vbscript)