Visual Basic

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

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

Adding Error Handling to the Signature Block Program

By Dan Mabbutt, About.com

Mar 9 2008

Although our Signature Block program works pretty well now, there's no error checking or exception handling in it. We'll add some now. While we're seeing another example of exception handling, we'll also learn about how to add a log file to your program.

One reason a lot of exception handling might not be as necessary in the Signature Block program is that all the data comes from pre-existing files such as the SigBlockData.XML file. All the user has to do is select options and items from the ComboBox control.

But to see how exception handling works, let's create an error by simply deleting the last line from the XML file: </SigBlocks>. This closes the SigBlocks element and is necessary for a valid XML file. This is the kind of error that you might make creating the file manually or could possibly even happen through a hardware error.

When we save this incorrect file and run the program, it blows up with an InvalidOperationException error without doing anything. This isn't a very satisfactory user experience!

To solve this problem, let's first code a Try - Catch structure for the Subs in the SigBlock class that process XML, Load, New and ReLoad. After examining the detail available when the program crashes, it appears that the Message property does have something useful in it:

Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Unexpected end of file has occurred. The following elements are not closed: SigBlocks. Line 31, position 1."

We first try simply coding a MessageBox provide better information to the user. Here's what that looks like:

Try
   Using reader As XmlReader = ...

<the rest of the existing code here until>
   End Using
Catch e As XmlException
   MessageBox.Show(e.Message)
End Try

When this code has been added to both methods, at least the program doesn't crash anymore. But testing reveals that after the MessageBox displays the error a few times, the problem goes away entirely! Examining the code reveals that when the XML file is rewritten in the ReLoad Sub, Microsoft's objects make up for the problem and write the correct closing element node. This "self healing" property might make for a more robust application.

Our second version substitutes a new technique, application logging, for the MessageBox.

Try
   Using reader As XmlReader = ...

<the rest of the existing code here until>
   End Using
Catch e As XmlException
   My.Application.Log.WriteException(e, _
   TraceEventType.Error, _
   "Exception in ExceptionLogTest " & ".")
End Try

Application Error Logging

Application logging, like exception handling, can include a lot of sophistication for applications that need it. Much of what you might find in the official documentation tells you how to create logging for more demanding requirements. In our example, the logging is about as simple as it can be done. The WriteException method creates the log in a default location that can be overridden but is typically in the local user Application directory. For example:

C:\Documents and Settings\Joe\Application Data\AboutVB\SigBlock\1.0.0.0

The log file contains these records after a test run:

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

In this case, some error checking was able to make the program self-correcting.

To download the source code for this version of SigBlock and the Exception Example program, click here.

Currently, this is the last segment of the Visual Basic .NET 2008 Express tutorial. If you have suggestions for how it can be improved, send them to me at visualbasic@about.com.

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  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.