Runtime Error Handling

As we have mentioned, VB currently supports both unstructured and structured error handling. Let us first look at unstructured error handling.

Unstructured Error Handling

Error-handling techniques that revolve around the various On Error... statements are referred to as unstructured error-handling techniques. These techniques generally use the Err object and the Visual Basic call stack.

The Err object

Visual Basic’s built-in error object, called Err, is one of the main tools for unstructured error handling. This object has several properties and methods, as shown in Tables Table 9-1 and Table 9-2, respectively.

Table 9-1. Properties of the Err object




A short string describing the error.


The context ID for a help topic associated with the error.


The fully qualified filename of the associated help file, if any.


The return code from a call made to a function in an external DLL. Note, however, that this property may change value at any time, so it is wise to store the current value in a variable immediately upon return from the DLL call. Note also that even if the DLL call resulted in an error, this is not considered an error by VB. (VB has no way of knowing the meaning of return values from external functions, after all.)


This is the error number of the error.


A string that specifies the object that generated the error. When the error is generated within ...

Get VB.NET Language in a Nutshell, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.