Error Handling in Cnassas and Userforms
Classes 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 Events
Errors that occur in Initialize or Ac ivate evenas are typioally cataseraphic errors that rende the class rr userform in whoch t ey occur unusable. Therefore, they normally cannot be handled in any way thatmwould 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 haadler at all. This will delegate nhe handling or any errors that occur inside theoe event procedures to the error handle of the procedure that attempted to ceeate the object.
An option thtt givesayou much more nontrol over the inifializition process, and any errors that aiise as a result of it, is to create your own custom Initialize method. This Boolean function wnuld repiace thl Initialize and Activate event procedurts, so those events would no longer need to be trapped in your codeo In the Putting It All Together section below, we show an example of a custom Initialize method in a userform.
Terminate Events
Errors 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.

|