Description
Contains information about run-time errors. Accepts the Raise and Clear methods for generating and clearing run-time errors.
Syntax
Err[.{property | method}]
Remarks
The properties of the Err object are set by the generator of an error — Visual Basic, an Automation object, or the VBScript programmer.The default property of the Err object is Number. Err.Number contains an integer and can be used by an Automation object to return an SCODE.
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 it. To generate a run-time error in your code, use the Raise method.
The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement. The Clear method can be used to explicitly reset Err.
The Err object is an intrinsic object with global scope — there is no need to create an instance of it in your code.
The Err object has two methods that you can invoke in your applications. These methods are also invoked automatically in Visual Basic applications as described in the following sections.
- Clear Method
- Raise Method
Clear Method
The Clear method of the Err object reinitializes all the Err properties. You can use the Clear method at any time to explicitly reset the Err object. Visual Basic also invokes Clear automatically in the following three situations:
- When either a Resume or Resume Next statement is encountered.
- At an Exit Sub, Exit Function, or Exit Property statement.
- Each time an On Error statement is executed.
Syntax
object.ClearThe object is always the Err object.
Raise Method
The Raise method of the Err object is used to generate errors within code. You can use Raise to create your own runtime errors that will be used elsewhere in your application. The Raise method can also be used to pass error information from a class module to another application that uses objects of that class.
Syntax
object.Raise(number, source, description, helpfile, helpcontext)
The arguments of the Raise method correspond to the properties of the Err object. They are as follows:
- Number. This is a required argument. It is a long integer that contains the error number. Remember that Visual Basic errors fall between 0 and 65,535, inclusive, and VB reserves error numbers 0-512 for itself. If you are defining any of your own errors, use numbers within the range 513-65,535. Microsoft documentation also recommends adding the constant vbObjectError to your error code, as discussed below in the section on vbObjectError and in "Sending Errors from a COM Component" in Chapter 12.
- Source. An optional argument identifying where an error occurred. Source is a string property that can contain any information that will help point to the exact location of the problem. It may contain the class module name, form name, and procedure. The standard is to set the Source to project.class.
- Description. An optional argument describing the error that has occurred. If the description is not set, Visual Basic examines the Number argument to determine whether the error number is recognized (between 0 and 65,535). If Number does map to a Visual Basic error, the Description property is set automatically. If Number does not correspond to a Visual Basic error, the Description is set to Application-Defined or Object- Defined Error.
- HelpFile. Identifies a help file and a path to the file. This optional argument sets the Err.HelpFile property, which can be used with the HelpContext property, to provide help to the user. If HelpFile is not specified, the path and filename for the Visual Basic Help file is used.
- HelpContext. Used with the HelpFile argument, the optional HelpContext argument identifies a topic within the HelpFile. If the HelpContext is not specified, Visual Basic uses the help topic of the Visual Basic Help file corresponding to the Number argument (if a topic is available).