Using Enum

If you want to raise your own specific errors and have code to handle these errors you could put everything into a dedicated class.
Define all your errors using an Enum.

Public enCustomErrors 
    lFirstError = -10000000 + 1024
    lSecondError
    lThirdError
End Enum

Use the vbObjectError built-in constant to ensure the custom error codes do not overlap with reserved/existing error codes.
The Err.Raise method can also take custom Description and Source parameters so it worth having a description associated with each error.

Private Const sDescription_FirstError As String = "This is the first type of error." 
Private Const sDescription_SecondError As String = "This is the second type of error."

Create a dedicated private method to raise each error:

Private Sub OnFirstError(ByVal source As String) 
    Err.Raise enCustomErrors.lFirstError, source, sDescription_FirstError
End Sub

Private Sub OnSecondError(ByVal source As String)
    Err.Raise enCustomErrors.lSecondError, source, sDescription_SecondError
End Sub

To raise the error, just call the correspnding subroutine.

Public Sub DoSomething() 
   If (True) Then
      Call OnFirstError("DoSomething")
   End If
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext