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

Property

Description

Description

A short string describing the error.

HelpContext

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

HelpFile

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

LastDLLError

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.)

Number

This is the error number of the error.

Source

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.