Error Handling in Class Modules

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You use class modules to create custom objects. When an error occurs in your class module, you do not want the error message to be raised in your class module, but rather you want to define a custom error and raise that error in the class module and send the error back to the calling procedure.

You can define custom errors by creating custom error numbers and assigning custom error descriptions. You then use the Raise method of the Err object to raise the custom error. You can use error numbers for your own errors in the range vbObjectError + 512 to vbObjectError + 65536. You can assign custom error values and associated error descriptions by using module-level constants in your class module. Then, use those values when raising errors to be returned to the calling procedure.

In this example, a class module, clsMailMessage, defines a custom object used to add a simple mail-messaging component to any Office application. Constants are used to define custom error numbers and descriptions, as shown in the following example.

' Custom error constants:
Private Const CUSTOM_ERR_SOURCE       As String = "clsMailMessage Object"
Private Const CUSTOM_ERR_ERRNUMBASE   As Long = vbObjectError + 512

' Invalid MailType custom errors:
Private Const CUSTOM_ERR_INVALID_MAILTYPE _
   As Long = CUSTOM_ERR_ERRNUMBASE + 1
Private Const CUSTOM_ERR_INVALID_MAILTYPE_DESC _
   As String = "MailItem Type argument must be 1 (olTo), 2 (olCC), or 3 (olBCC)."
   
' Unable to initialize Outlook errors:
Private Const CUSTOM_ERR_OUTLOOKINIT_FAILED _
   As Long = CUSTOM_ERR_ERRNUMBASE + 2
Private Const CUSTOM_ERR_OUTLOOKINIT_FAILED_DESC _
   As String = "Could not initialize Microsoft Outlook."
   
' Unable to send mail errors:
Private Const CUSTOM_ERR_SENDMAIL_FAILED _
   As Long = CUSTOM_ERR_ERRNUMBASE + 3
Private Const CUSTOM_ERR_SENDMAIL_FAILED_DESC _
   As String = "Unable to send mail message."

Procedures within the class module use these values as arguments to the Raise method of the Err object to raise custom errors. For example, the following procedure raises a custom error if an invalid value is passed to the procedure in the intType argument:

Public Function MailAddRecipient(strName As String, _
                               Optional intType As Integer = olTo) As Boolean
                        
   ' This procedure adds a recipient name and MailItem object type specifier
   ' to the p_strRecipients() array. This array is used in the CreateMail
   ' function to add recipients to the MailItem object's Recipients collection.
   
   Static intToCntr As Integer
      
   ' Validate intType argument.
   If intType > 3 Or intType < 1 Then
      ' Raise error: Type argument must be olTo (or 1),
      ' olCC (or 2), or olBCC (or 3) only.
      Err.Raise Number:=CUSTOM_ERR_INVALID_MAILTYPE, _
         Source:=CUSTOM_ERR_SOURCE, _
         Description:=CUSTOM_ERR_INVALID_MAILTYPE_DESC
      MailAddRecipient = False
      Exit Function
   End If
   
   ReDim Preserve p_strRecipients(1, intToCntr)
   
   p_strRecipients(0, intToCntr) = strName
   p_strRecipients(1, intToCntr) = intType
   
   intToCntr = intToCntr + 1
End Function

Note   You cannot use the Raise method to return an error to the calling procedure if you have error handling turned on in the procedure where the error occurs. If error handling is on, you must use the Raise method again within the class module error handler.

See Also

Writing Error-Free Code | Design-Time Tools | Run-Time Tools | Script Debugging Tools | Basic Error Handling | Getting Information About an Error | Handling Script Errors | Logging Errors