Determine Error Handling State

Lee W, MVP
Lee W, MVP used Ask the Experts™
on
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:
ON ERROR GOTO Label

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:
ON ERROR RESUME NEXT

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:
ON ERROR RESUME NEXT
X = 5
IF X = "bob" THEN X = "Bob"
IF ERR.NUMBER <> 0 THEN
   'Handle the error
END IF 

Open in new window


And to disable graceful error handling, you use
ON ERROR GOTO 0

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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

ErrorRoutine:
    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

Software Team Lead
Commented:
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?

can't really tell that, but we can set variables to determine the error handling state. Similar to what Martin has posted above.
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Author

Commented:
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.
   Else
      '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.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Author

Commented:
Yes, that was my intention.  But I did try to indicate appreciation for your comment too!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial