Visual Basic has included error handling since its initial release
On Error statement.
Although often derided by developers, this mechanism did effectively
catch and process all errors when used properly. Visual Basic 2005 still
includes this error-handling methodology, but it also includes
structured error handling, new with .NET. This
chapter considers this new error-processing system, comprised of the
Try…Catch…Finally statement and
Although you’ve been a Visual Basic 6.0 developer for years, and
you’ve already used
statements in your Visual Basic 2005 code, you want to try out the
structured error-handling statements you’ve heard so much
block statement to locally monitor and handle errors.
The statement has three sections:
The code you need to monitor for errors appears in this first section.
When an error occurs, processing jumps immediately from
Try section to a matching
Catch block (We’ll define
“matching” shortly). Any remaining unprocessed statements in the
Try block are ignored. You
can have any number of
entries in your error-handling block.
Any code you include in this optional section runs whether
an exception occurs or not. It’s a useful place to put any
cleanup code related to resources you allocated in the
Here’s the syntax of the