Link to home
Start Free TrialLog in
Avatar of Lee W, MVP
Lee W, MVPFlag for United States of America

asked on

Determine Error Handling State

Is there any way to determine what the current error handling is in vb6?

Default error handling crashes the program displaying the error message.

If you use:

Open in new window

The program goes to the point in the code with the label and continues executing (presumably, you have defined what to do here to mitigate the error)

If you use:

Open in new window

The program will continue ignoring errors until it either can't run at all, you handle the errors, or you disable the resume next option.  I often (but not always) use ON ERROR RESUME NEXT to allow the program to continue and then I explicitly check for an error number and handle the error there.  For example:
X = 5
IF X = "bob" THEN X = "Bob"
   'Handle the error

Open in new window

And to disable graceful error handling, you use

Open in new window

But is there any way to tell if graceful error handling is on?  Any way to tell that the last time an ON ERROR statement executed it was set to go to a label, resume next, or goto 0?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I don't understand why you'd want to know that, but something like this?
Private Sub CatchErrors()

Dim strWhere As String

    strWhere = "A"
    On Error GoTo ErrorRoutine

    ' some code where On Error Resume Next is appropriate
    strWhere = "B"
    On Error GoTo 0

    Exit Sub

    Select Case strWhere
        Case "A"
            MsgBox "Error at " & strWhere
        Case "B"
            MsgBox "Graceful error at " & strWhere
            Resume Next
    End Select
End Sub

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lee W, MVP


Thanks, that's what I was afraid of.

Reason being, I've taken over the coding of a (relatively) large application for a client and the previous coder(s) enabled and disabled error checking at times.  And then I've done likewise (this is, by far, the largest VB6 project I've ever worked on).  So I might have a sub routine or a function somewhere that I want to hard error out but I don't know if error checking is enabled somewhere in the earlier part of the call stack.  I could set a variable every time I use "ON ERROR RESUME NEXT" and then when I turn it off, I can change the variable and test for it later, I was just hoping I could do something like this:

Sub Main
   On Error Resume Next
   Call Login
   Call GetRegSettings
   Call DisplayForm
   ON Error Goto 0
End Sub

Sub Login
   'Do stuff
   If Err.ErrorCheckingState = vbErrResume Then  'For reference, Err.ErrorCheckingState is not real... sorry anyone reading this looking for a similar answer!
      EnableErrorChecking = True
   End If
   On Error Goto 0    'Disable error trapping for this part
   'Do More stuff
   If EnableErrorChecking = True Then 
      On Error Resume Next  'Error trapping was on - re-enable it.
      'Do nothing - error checking wasn't on to begin with!
   End If
End Sub

Open in new window

I suppose I could write my own error checking enabling/disabling function but that would then require going through all the code to change it.  Not impossible, but a little tedious.
Was it your intention to award the points to Ryan? If so no problem; you win some you lose some, but I just wanted to check.
Yes, that was my intention.  But I did try to indicate appreciation for your comment too!