We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

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.

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.


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.

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




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


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


Gets the Exception instance that caused the current exception.


Gets a message that describes the current exception.


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


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


Gets the method that throws the current exception.