Chapter 9
Trapping Errors by Using Structured Error Handling
After completing this chapter, you will be able to:
Manage run-time errors by using the Try…Catch error handler.
Create a disc drive error handler that tests specific error conditions by using the Catch When statement.
Write complex error handlers that use the Err object and Err.Number and Err.Description properties to identify exceptions.
Build nested Try…Catch statements.
Use error handlers in combination with defensive programming techniques.
Leave error handlers prematurely by using the Exit Try statement.
In Chapter 8, “Debugging Visual Basic Programs,” you learned how to recognize run-time errors in a Microsoft Visual Basic program and how to locate logic errors and other defects in your program code by using the Microsoft Visual Studio 2005 debugging tools. In this chapter, you'll learn how to build blocks of code that handle run-time errors, also referred to as exceptions, which occur as a result of normal operating conditions—for example, errors due to a CD not being in a drive, a lost Internet connection, or an offline printer. These routines are called structured error handlers (or structured exception handlers), and you can use them to recognize run-time errors as they occur in a program, suppress unwanted error messages, and adjust program conditions so that your application can regain control and run again.
Fortunately, Visual Basic offers the powerful Try…Catch code block for handling errors. In this chapter, you'll learn how to trap run-time errors by using Try…Catch code blocks, and you'll learn how to use the Err.Number and Err.Description properties to identify specific run-time errors. You'll also learn how to use multiple Catch statements to write more flexible error handlers, build nested Try…Catch code blocks, and use the Exit Try statement to exit a Try…Catch code block prematurely. The programming techniques you'll learn were added to Visual Basic with Microsoft Visual Studio .NET versions 2002 and 2003, and they are similar to the structured error handlers provided by the most advanced programming languages, such as Java and C++. The most reliable, or robust, Visual Basic programs make use of several error handlers to manage unforeseen circumstances and provide users with consistent and trouble-free computing experiences.
Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005
If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Microsoft Visual Basic 2005, including the following:
The Try…Catch code block is a new mechanism for writing structured error handlers. Although you can still use Visual Basic 6 error-handling keywords, including On Error Goto, Resume, and Resume Next, the Try…Catch syntax avoids the potential complications of Goto constructions and offers a very efficient way to manage run-time errors.
The Catch When statement can be used to test specific program conditions and handle more than one run-time error in a Try…Catch code block.
The Exit Try statement offers a new way to exit structured error handlers.
Visual Studio 2005 continues to provide the Err.Number and Err.Description properties to identify run-time errors. In addition, you can use the new Err.GetException method to return information about the underlying error condition, or exception, that halted program execution.