Re: Error handling in VB6

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The VB syntax for catching and handling errors is definitely not
"structured", but the internal Windows support for exceptions (which isn't
VB-specific) is structured

In principle, you could set a single exception handler (i.e. "On Error Goto
label" in VB6 terms) in your root Main() procedure, and that would catch all
exceptions thrown in Main(), and in anything called from Main().

Unfortunately, this ideal doesn't work for event-driven VB programs - which
happens to be the vast majority. For some reason (possibly one of
'determinism'), exceptions are not thrown out of event handlers back to
whatever is next on the run-time stack. Any exception in an event handler
therefore ends up being fatal to the program. The only way to avoid this is
to always have an exception error in each event handler.

Tony Proctor

"MikeD" <nobody@xxxxxxxxxxx> wrote in message
news:eLoZt4ffIHA.2268@xxxxxxxxxxxxxxxxxxxxxxx

"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




.



Relevant Pages

  • Re: Error handling in VB6
    ... Private Sub DoThis() ... OnError GoTo SomeErrHandler ... VB6 has no such thing as "truly" structured error handling. ... VB6 has no such thing as a global error handler. ...
    (microsoft.public.vb.general.discussion)
  • Re: What is the story with Server Busy
    ... GoTo -1' then it will cancel the fact you're "handling" an error. ... When you fix that, 80% of your remaining problems will be from just one ... At ErrLab, VB deems that an error handler is active, and so the next 'On ...
    (microsoft.public.vb.enterprise)
  • Re: What do you think about the code?
    ... Using gotos to simulate exceptions would be as close as one ... Within a fixed scope, but you still identify the error handler (the ... I'd chuck in a goto rather than have the logic code do ...
    (comp.lang.c)
  • Re: File Descriptors
    ... At least one Tcl ... I think that depends somewhat one where you are coming to tcl/tk from. ... With regard to error handling in particular, ... Conditions under which exceptions occur ...
    (comp.lang.tcl)
  • Re: Exception as the primary error handling mechanism?
    ... handling rather than use error code. ... In the article API Design Matters by Michi Henning ... handling exceptions is almost always slower than testing a return ... could some python expert explain to me why exception is ...
    (comp.lang.python)