Re: Error handling in VB6
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Tue, 4 Mar 2008 08:46:38 -0500
"Ronald Raygun" <invalid@xxxxxxxxxx> wrote in message
news:bsCdnaCcXftSqVDanZ2dneKdnZydnZ2d@xxxxxxxxx
Currently, the error handling in my VB6 app is very unstructured - it
could be that I still haven't got my head around VB6 way of handling
errors. It just dosen't "feel right" at the moment - I keep feeling that
an error can "slip by" somehow, and crash my application.
My routines generally look either like this:
Private Sub DoThis()
'decl here
OnError GoTo SomeErrHandler
...
Exit Sub
SomeErrHandler:
MsgBox err.Description, vbCritical, APP_NAME
End sub
OR this
Private Sub DoThat()
'decl here
OnError Resume Next
...
if err.Number <> 0 GoTo SomeErrHandler
Exit Sub
SomeErrHandler:
MsgBox err.Description, vbCritical, APP_NAME
End Sub
First thing I would like to do is to have a 'global' Error catcher in my
application that provides a last resort error catcher, that catches any
unhandled errors.
Secondly, is this (above), the recommended way of handling VB6 errors - or
does someone know of a better (i.e. more robust, "failsafe") way?
2 things up front:
1. VB6 has no such thing as "truly" structured error handling.
2. VB6 has no such thing as a global error handler.
Best thing you can do to make sure EVERY error is handled in some manner is
to put an error handler in EVERY event procedure which contains code. Keep
in mind that if you have an active error handler in an event procedure, that
error handler is active in any procedures/functions you call from that event
procedure if the procedure/function does not have its own error handler.
Personally, I only use On Error Resume Next in "special circumstances".
Usually, the special circumstance is when it's OK to simply ignore the error
or if on the very next line of code, I need to check for a specific error
number. After that special circumstance is over, I go back to the "normal"
error handling. Something like this (air code):
Private Function MyFunc() As Long
On Error GoTo EH
<code>
On Error Resume Next
(1 or 2 lines, at most, of code>
If Err.Number = <specific error number> Then
<code for special handling of this error number>
End If
On Error GoTo EH
<code>
Exit Function
EH:
<error handling code>
End Function
I would never use On Error Resume Next and then GoTo my error handler, but
that's more personal preference than right or wron (although IMO, it's
extremely unstructured). If you're going to do as you show in your 2nd
example, you might as well litter your code with other GoTos.
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: Error handling in VB6
- From: Tony Proctor
- Re: Error handling in VB6
- References:
- Error handling in VB6
- From: Ronald Raygun
- Error handling in VB6
- Prev by Date: Re: A LIttle OT: Re: Stop user pushing prt-sc
- Next by Date: Re: Separating code from UI
- Previous by thread: Error handling in VB6
- Next by thread: Re: Error handling in VB6
- Index(es):
Relevant Pages
|