Visual Basic Concepts

Debugging Class Modules

See Also

Debugging class modules differs slightly from debugging ordinary programs. This is because an error in a property or method of a class module always acts like a handled error. (That is, there's always a procedure on the call stack that can handle the error — namely the procedure that called the class module's property or method.)

Visual Basic compensates for this difference by providing the error-trapping option Break in Class Module, in addition to the older options Break on Unhandled Errors and Break on All Errors.

Note   You can set the Default Error Trapping State on the General tab of the Options dialog box, available from the Tools menu. The option you select affects the current session, and becomes the default for all subsequent instances of Visual Basic. To change the setting only for the current session, without affecting the default, select Toggle from the Code window context menu (which is available by right-clicking on the Code window).

For example, suppose the class module Class1 contains the following code:

Public Sub Oops()
   Dim intOops As Integer
   intOops = intOops / 0
End Sub

Now suppose a procedure in another class module, form, or standard module calls the member Oops:

Private Sub Command1_Click()
   Dim c1 As New Class1
   c1.Oops
End Sub

If the error trapping option is set to Break on Unhandled Errors, execution will not stop on the zero divide. Instead, the error will be raised in the calling procedure, Command1_Click. Execution will stop on the call to the Oops method.

You could use Break on All Errors to stop in the zero divide, but Break on All Errors is a very inconvenient option for most purposes. It stops on every error, even errors for which you've written error handling code.

Break in Class Module is a compromise setting:

Tip   When you hit a break point using Break in Class Module or Break on All Errors, you can step or run past the error — into your error handling code or into the code that called procedure in which the error occurred — by pressing ALT+F8 or ALT+F5.

For More Information   Debugging is discussed in detail in "Debugging Your Code and Handling Errors."