No matter how carefully crafted your code, errors can (and probably will) occur. Ideally, Visual Basic procedures wouldn't need error-handling code at all. Unfortunately, sometimes files are mistakenly deleted, disk drives run out of space, or network drives disconnect unexpectedly. Such possibilities can cause run-time errors in your code. To handle these errors, you need to add error-handling code to your procedures.
Sometimes errors can also occur within your code; this type of error is commonly referred to as a bug. Minor bugs — for example, a cursor that doesn't behave as expected — can be frustrating or inconvenient. More severe bugs can cause an application to stop responding to commands, possibly requiring the user to restart the application, losing whatever work hasn't been saved.
The process of locating and fixing bugs in your application is known as debugging. Visual Basic provides several tools to help analyze how your application operates. These debugging tools are particularly useful in locating the source of bugs, but you can also use the tools to experiment with changes to your application or to learn how other applications work.
This chapter shows how to use the debugging tools included in Visual Basic and explains how to handle run-time errors — errors that occur while your code is running and that result from attempts to complete an invalid operation.
How to Handle Errors An overview of error handling in Visual Basic. |
|
Designing an Error Handler An introduction to error trapping procedures. |
|
Error Handling Hierarchy A discussion of the sequence of events when an error occurs. |
|
Testing Error Handling by Generating Errors Methods for creating intentional errors to test your code. |
|
Inline Error Handling Adding error handling within a procedure. |
|
Centralized Error Handling Adding a error handling sub procedure to your application. |
|
Turning Off Error Handling Methods for disabling error handling. |
|
Error Handling with ActiveX Components Considerations for handling errors returned by other objects. |
|
Approaches to Debugging An introduction to debugging. |
|
Avoiding Bugs Tips for error-free coding. |
|
Design Time, Run Time, and Break Mode A discussion of Visual Basic's modes. |
|
Using the Debugging Windows Introducing the debugging tools in Visual Basic. |
|
Using Break Mode Testing your code's execution with breakpoints. |
|
Running Selected Portions of Your Application Debugging selected code segments. |
|
Monitoring the Call Stack Tracing your application's execution with the Call Stack. |
|
Testing Data and Procedures with the Immediate Window Using the Immediate window for interactive debugging. |
|
Special Debugging Considerations A discussion of events that can affect debugging. |
|
Tips for Debugging Some ideas to help simplify debugging. |
Errors.vbp Many of the code samples in this chapter are taken from the Errors.vbp sample application. You'll find this application listed in the Samples directory. |