Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

228 Part II Programming Fundamentals


in Chapter 8. It’s not that Visual Basic isn’t smart enough to handle the glitch; it’s just that the
program hasn’t been “told” what to do when something goes wrong.

Fortunately, you don’t have to live with occasional errors that cause your programs to crash.
You can write special Visual Basic routines, called structured error handlers, to manage and
respond to run-time errors before they force the Visual Basic compiler to terminate your
program. An error handler handles a run-time error by telling the program how to continue
when one of its statements doesn’t work. Error handlers can be placed in each event procedure
where there is potential for trouble, or in generic functions or subprograms that receive control
after an error has occurred and handle the problem systematically. (You’ll learn more about
writing functions and subprograms in Chapter 10, “Creating Modules and Procedures .”)

Error handlers handle, or trap, a problem by using a Try... Catch code block and a special
error-handling object named Exception. The Exception object has a Message property
that you can use to display a description of the error. For example, if the run-time error
is associated with loading a file from a CD or DVD drive, your error handler might display
a custom error message that identifies the problem and prompts the user to insert a CD
or DVD, rather than allowing the failed operation to crash the program.

When to Use Error Handlers


You can use error handlers in any situation where an action (either expected or unexpected)
has the potential to produce an error that stops program execution. Typically, error handlers
are used to manage external events that influence a program—for example, events caused by
a failed network or Internet connection, a CD, DVD, or diskette not being inserted correctly
in the drive, or an offline printer or scanner. Table 9-1 lists potential problems that can be
addressed by error handlers.

TABLE 9-1 Potential Problems for Error Handlers
Problem Description
Network/Internet
problems

Network servers, Internet connections, and other resources that fail, or
go down, unexpectedly.
Database problems Unable to make a database connection, a query can’t be processed or
times out, a database returns an error, and so on.
Disc drive problems Unformatted or incorrectly formatted CDs, DVDs, diskettes, or media
that aren’t properly inserted; bad sectors, CDs, DVDs, or diskettes that
are full; problems with a CD or DVD drive; and so on.
Path problems A path to a necessary file that is missing or incorrect.
Printer problems Printers that are offline, out of paper, out of memory, or otherwise
unavailable.
Software not installed A file or component that your application relies on but that is not
installed on the user’s computer, or an operating system incompatibility.
Security problems An application or process that attempts to modify operating system files,
use the Internet inappropriately, or modify other programs or files.
Free download pdf