Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
227

Chapter 9


Trapping Errors by Using


Structured Error Handling


After completing this chapter, you will be able to:
n Manage run-time errors by using the Try... Catch error handler.
n Create a disc drive error handler that tests specific error conditions by using the Catch
statement.
n Write complex error handlers that use the Exception object and the Message property.
n Build nested Try... Catch statements.
n Use error handlers in combination with defensive programming techniques.
n 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 2010 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 or DVD not being in an optical drive, a broken 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, 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 Exception object 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 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 use several error handlers
to manage unforeseen circumstances and provide users with consistent and trouble-free
computing experiences.

Processing Errors by Using the Try Catch Statement


A program crash is an unexpected problem from which a program can’t recover. You might
have experienced your first program crash when Visual Basic couldn’t load artwork from
a file, or when you intentionally introduced errors into your program code during debugging

Table of Contents


Trapping Errors by Using
Structured Error Handling............................ 227
Processing Errors by Using the Try... Catch Statement................. 227
When to Use Error Handlers.................................... 228
Setting the Trap: The Try... Catch Code Block.................... 229
Path and Disc Drive Errors...................................... 229
Writing a Disc Drive Error Handler.................................... 233
Using the Finally Clause to Perform Cleanup Tasks............... 234
More Complex Try... Catch Error Handlers...................... 236
The Exception Object.......................................... 236
Specifying a Retry Period....................................... 239
Using Nested Try... Catch Blocks............................... 242
Comparing Error Handlers with Defensive
Programming Techniques............................................ 242
One Step Further: The Exit Try Statement....................... 243
Chapter 9 Quick Reference........................................... 244

Free download pdf