Freigeben über


Visual Basic Concepts

Using Break on Error in Components

You can change the way Visual Basic enters break mode when an error occurs in your component by setting the Error Trapping option in your component project.

In your component project, choose Options from the Tools menu to open the Options dialog box, and select the General tab. There are three options in the Default Error Trapping State box, as described below.

Note   The setting in the Default Error Trapping State box controls the default setting of the option when you start Visual Basic; it also toggles the option in the current session. If you wish to change the setting for the current session only, without affecting the default, select Toggle from the code window context menu.

Suppose you have a component that provides a Widget object that has a Spin method. The following descriptions assume that the test application has called the Spin method of the Widget object, and that an error has occurred in the Spin method’s code.

  • Break on All Errors: The component project is activated, and the Spin method’s code window receives the focus. The line of code that caused the error is highlighted. Visual Basic always enters break mode on such an error, even if error handling is enabled in the Spin method.

    Note   You can press Alt+F8 or Alt+F5 to step or run past the error.

  • Break in Class Module: If error handling is not enabled in the Spin method, or if you are deliberately raising an error for the client by calling the Raise method of the Err object in the Spin method’s error handler, the component project is activated, and the Spin method’s code window receives the focus. The line of code that caused the error is highlighted.

    Note   You can press Alt+F8 or Alt+F5 to step or run past the error.

    If error handling is enabled in Spin, then the error handler is invoked. As long as you don’t raise an error in the error handler, Visual Basic does not enter break mode.

  • Break on Unhandled Errors: Visual Basic never enters break mode in properties or methods of the component. If error handling is not enabled in the client procedure that called the Spin method, execution stops on the line of code that made the call.

    To understand the behavior of Break on Unhandled Errors in a component project, remember that the component’s properties and methods are always called by somebody else. An error in a property or method can always be handled by passing it up the call tree into the client procedure that called the property or method.

    Note   When an out-of-process component enters break mode, focus may not immediately switch to the component project. If you click anywhere on the client, the Component Busy dialog box will be displayed. Click the Switch To button to give the focus to the component project.

For More Information   See "Debugging Your Code and Handling Errors" in the Visual Basic Programmer’s Guide.