Home > database >  VBA: How to Determine if vbObjectError Was Used?
VBA: How to Determine if vbObjectError Was Used?

Time:12-10

I'm trying to create a centralized error handler. I'm using vbObjectError as recommended. But i can't figure out how to compose a conditional which can determine if my error is native or custom.

The desired IsCustomErr should work without modification even if i later add additional custom errors to the enum.

Enum CustomEr
          LaidEgg = vbObjectError
          UserEaten
          Paused
          Cancelled
End Enum


Sub Test
          On Error Goto HANDLER
          Err.Raise LaidEgg
          Exit Sub
HANDLER:
          GlobalHandler
End Sub


Sub GlobalHandler
          If IsCustomErr Then MsgBox "Custom"
End Sub


Function IsCustomErr()As Boolean
' ONE OF THESE?
          With Err
                    IsCustomErr = .Number < 0
                    IsCustomErr = .Number >= vbObjectError
                    IsCustomErr = (.Number >= vbObjectError) And (.Number < 0)
                    IsCustomErr = .Number Or vbObjectError
                    IsCustomErr = TwosComplement(.Number) Or TwosComplement(vbObjectError)
          End With
End Function

CodePudding user response:

This seems viable:

Enum CustomEr
    [_First]
    laidegg
    UserEaten
    paused
    Cancelled
    [_Last]
End Enum

Sub Test1()
    Debug.Print "Test1"
    On Error GoTo HANDLER
    Err.Raise vbObjectError   laidegg
    Exit Sub
HANDLER:
    GlobalHandler
End Sub

Sub Test2()
    Debug.Print "Test2"
    On Error GoTo HANDLER
    Debug.Print 1 / 0
    Exit Sub
HANDLER:
    GlobalHandler
End Sub

Sub GlobalHandler()
    Debug.Print "Custom?", IsCustomErr()
End Sub

Function IsCustomErr() As Boolean
    Dim v As Long
    For v = CustomEr.[_First] To CustomEr.[_Last]
      If (Err.Number - vbObjectError) = v Then
           IsCustomErr = True
           Exit For
      End If
    Next v
End Function


CodePudding user response:

Too much to fit as a comment, but this is working for me:

Enum CustomEr
          LaidEgg = vbObjectError
          UserEaten
          Paused
          Cancelled
End Enum

Function IsCustomErr() As Boolean
    IsCustomErr = Err.Number >= CustomEr.LaidEgg And Err.Number <= CustomEr.Cancelled
End Function

Sub Example()

    On Error Resume Next
    Err.Raise CustomEr.UserEaten
    Debug.Print IsCustomErr
    'True
    
    Err.Clear
    
    Err.Raise 1
    Debug.Print IsCustomErr
    'False
End Sub

A possible reason that it doesn't work for you might be that you are Ending execution before moving to the handler. From the article on the Err Object:

The Err object's properties are reset to zero or zero-length strings ("") after an Exit Sub, Exit Function, Exit Property, or Resume Next statement within an error-handling routine.

Another potential issue is that vbObjectError is -2147221504 so by checking Err.Number >= vbObjectError you will return True for every number larger than vbObjectError which is basically all of them.

  • Related