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
Too much to fit as a comment, but this is working for me:
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:
Another potential issue is that vbObjectError is
-2147221504so by checkingErr.Number >= vbObjectErroryou will returnTruefor every number larger thanvbObjectErrorwhich is basically all of them.