Share via


How to: Retrieve Information from an Error Object (Visual Basic)

When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle the error.

The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement and after an Exit Sub or Exit Function statement within an error-handling routine. The Clear method can be used to explicitly reset Err.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To retrieve information from an error object

  1. You can filter for specific errors. This example checks to see if the error is a FileNotFound error and reacts if it is.

    If Err.Number = 53 Then
      MsgBox("File Not Found")
    End If
    
  2. You can also examine specific properties of the error object, such as Description, Erl, HelpContext, Helpfile, LastDLLError, Number, and Source. This example displays the description in a message box.

    MsgBox(Err.Description)
    

See Also

Reference

Err

Description

Number

HelpFile

HelpContext

LastDllError

Source

Concepts

Choosing When to Use Structured and Unstructured Exception Handling (Visual Basic)