Exception Class in Visual Basic

To facilitate the use of structured exception handling, Visual Basic provides the ability to separate standard code from exception handling code. Exception handling code accesses an instance of the Exception class, which allows you to retrieve information about any exception you encounter.

Remarks

Whenever an exception is thrown, the global Err object is set, and a new instance of an Exception class is created.

The properties of the Exception class aid in identifying the code location, type, and cause of exceptions. For example, the StackTrace property lists the called methods that led up to the exception, helping you find where the error occurs in the code. The Message property returns a text message describing the error; you can alter it to make a cryptic message easier to understand. If you do not supply an error message text string, the default is used. HelpLink gets or sets a link to an associated help file. Source gets or sets a string containing the name of the object causing the error or the name of the assembly where the exception originated.

Note

Avoid writing code that tests for specific error messages, since messages can change between versions of the class that originally produced them. Instead, test for the exception type or use the exception number from the global Err object.

Exception Class Properties

The following table lists and describes properties of the Exception class.

Property

Description

HelpLink

Gets or sets a link to the help file associated with this exception.

HResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

InnerException

Gets the Exception instance that caused the current exception.

Message

Gets a message that describes the current exception.

Source

Gets or sets the name of the application or object that caused the error.

StackTrace

Gets a string representation of the frames on the call stack at the time the current exception was thrown.

TargetSite

Gets the method that throws the current exception.

See Also

Tasks

Troubleshooting Exception Handling

Concepts

Introduction to Exception Handling

Types of Errors

Structured Exception Handling Overview for Visual Basic

Unstructured Exception Handling Overview

Other Resources

Exception Handling Tasks