Err Object |
The Err object contains properties and methods that allow you to obtain information about a single runtime error in a Visual Basic program. It also allows you to generate errors and to reset the error object. Because the Err object is an intrinsic object (which means that it's part of every VB project you create) with global scope, you don't need to create an instance of it within your code.
When an error is generated in your application—whether it's handled or not—the properties of the Err object are assigned values you can then access to gain information about the error that occurred. You can even generate your own errors explicitly using the Err.Raise method. You can also define your own errors to unify the error-handling process.
When your program reaches an Exit Function, Exit Sub, Exit Property, Resume, or On Error statement, the Err object is cleared and its properties reinitialized. This can also be achieved explicitly using the Err.Clear method.
Property Name | Description |
---|---|
Description | The string associated with the given error number. |
HelpContext | A context ID within a VB Help file. |
HelpFile | The path to a VB Help file. |
LastDLLError | The last error code generated by a DLL; available only on 32-bit Windows systems. |
Number | A long integer used to describe an error (i.e., an error code). |
Source | Either the name of the current project or the class name of the application that generated the error. |
Method Name | Description |
---|---|
Clear | Resets all the properties of the Err object. |
Raise | Forces an error of a given number to be generated. |
You may come across legacy code that has been written with either the Err function or the Err statement, both of which simply returned the error number. This code still runs because the Err object's default property is the Number property. As a result, simply using Err in your code (as you do when you call either the Err function or the Err statement) is the same as writing Err.Number.
The Visual Basic Err object isn't a collection; it contains only information about the last error, if one occurred. You could, however, implement your own error-collection class to store a number of errors by copying error information from the Err object into an application-defined error-collection object.
The Error statement, which was used in earlier versions of VB to generate an error, is included only in the language for backward compatibility and shouldn't be used in new code. Use the Err.Raise method instead.
An Err object can't be passed back from a class module to a standard code module.
For a full description of error trapping and error handling, see Chapter 6.