1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - Errors: Preventing and Handling Them
Structured Exception Handling

By , About.com Guide

Mar 9 2008

Before .NET, Visual Basic used the On Error statement to handle internal program errors. But just like we now use structured programming with .NET, we have also moved up from the bad old ways of VB6 to structured exception handling in .NET.

So what's structured exception handling?

On MSDN, you'll find this definition:

"An exception is any error condition or unexpected behavior encountered by an executing program."

It sounds like just the definition of an error, but an exception is an object - like everything else in .NET - so exceptions have properties and methods just like other objects. Because they're objects, you can work with them in code in a much more sophisticated way. And you can also create your own exception objects; you can pass them to other programs; and .NET itself has built in procedures for dealing with exceptions.

There are basically two types of exceptions: runtime exceptions and application exceptions. Your program can recover from some application exceptions, but in general, it can't recover from runtime exceptions. An example of a program that would issue a runtime exception would be a program that has too many nested calls (recursive programs have this problem frequently). This would result in a StackOverflowException.

The goal is to be able to add the right code to your program to handle all errors completely and then let your program continue. Even if you can't figure out how to do that, it might still be possible to get new data from the user. For example, a different name or path for a file that can't be found. If nothing else works, the program can at least exit as gracefully as possible. The program should never just crash.

The VB.NET Exception Object

VB6 only had what is called the "On Error" block - a good example of unstructured error handling. One problem with VB6 unstructured error handling is that when an error happens, your program is only passed an error code. An error code is just that - a code. If it's obvious what went wrong, such as a divide by zero error when your program only has one calculation in it, then this code might be useful. But a lot of the time, the code doesn't give you enough information to understand what went wrong.

In contrast, .NET passes your program an exception object (The documentation calls this "throwing" the exception.) with a lot of information packed into it. The following illustration shows the properties and methods for the StackOverflowException object as shown in the VB Express Object Browser.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Try-Catch-Finally blocks are the way VB.NET structured error handling processes errors. The next page explains how that works.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - Errors: Preventing and Handling Them

©2009 About.com, a part of The New York Times Company.

All rights reserved.