Err Object in Unstructured Exception Handling (Visual Basic)

To write error-handling code, you need to know which error occurred. In unstructured exception handling, you can find this out by using Err.

The Err object contains properties whose values are determined by the error that just occurred. The Number property contains the cause of the error, and the Description property contains a text message that describes the error in detail. Helpfile and HelpContext allow you to display a related help file when a user presses the Help button or the F1 key. LastDLLError displays the last DLL called, and whether or not the call was successful. Source specifies a string expression representing the object or application that generated the error.

Your error handler should test or save the relevant property values before any other error can occur. Your code can handle only a single error at a time, so you must deal with one before moving on to another.

Note

The Err object can be used only with exceptions caught with the On Error GoTo statement.

Application-Specific Errors

In addition to handling errors generated by Visual Basic, you might also want to handle errors specific to your application.

If you create an object that accesses other objects, you should write code to deal with any unhandled errors the other objects pass back. If necessary, you can map the error codes in Err.Number to one of your own application-specific errors, and then pass it back to the caller of your object.

See Also

Reference

On Error Statement (Visual Basic)

Description

Number

HelpFile

HelpContext

LastDllError

Source

Concepts

Introduction to Exception Handling (Visual Basic)

Types of Errors (Visual Basic)

Structured Exception Handling Overview for Visual Basic

Unstructured Exception Handling Overview (Visual Basic)