Error Handling in Classes and UserformsClasses and userforms present some unique error handling challenges that we cover in this section. As explained previously, event procedures in classes and userforms should almost always be considered entry point procedures. The Initialize, Activate and Terminate events are exceptions to this rule. The user does not directly trigger these events. Instead, they are fired as a side effect of a class being created or destroyed or a userform being created, shown or destroyed. Error handling for these events is a little tricky, so we discuss them in detail. Initialize and Activate EventsErrors that occur in Initialize or Activate events are typically catastrophic errors that render the class or userform in which they occur unusable. Therefore, they normally cannot be handled in any way that would mitigate them. If you are going to place code in either of these event procedures, the best option is not to give them an error handler at all. This will delegate the handling of any errors that occur inside these event procedures to the error handler of the procedure that attempted to create the object.Putting It All Together section below, we show an example of a custom Initialize method in a userform. Terminate EventsErrors that occur in Terminate events are unusual in that, assuming proper programming techniques have been used, neither are they catastrophic nor can they be mitigated. When the Terminate event is fired, the class or userform has performed its function and is being destroyed. If you need to place code in the Terminate event of a class or userform, it is best to simply ignore any errors that occur by using the On Error Resume Next statement at the beginning of the procedure. ![]() |