Export (0) Print
Expand All

The Exception Class

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.

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.

See Also

Introduction to Exception Handling | Types of Errors | Structured Exception Handling | Unstructured Exception Handling | The Err Object | Exception Class | Source Property

© 2014 Microsoft