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 NEXTX = 5IF X = "bob" THEN X = "Bob"IF ERR.NUMBER <> 0 THEN 'Handle the errorEND IF
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?
VB ScriptVBAVisual Basic Classic
Last Comment
Lee W, MVP
8/22/2022 - Mon
Martin Liss
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 SubErrorRoutine: Select Case strWhere Case "A" MsgBox "Error at " & strWhere Case "B" MsgBox "Graceful error at " & strWhere Resume Next End SelectEnd Sub
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 0End SubSub 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 IfEnd Sub
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 Liss
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.
Open in new window