Err Object.
Generates a run-time error.
object.Raise(Number, Source, Description, HelpFile, HelpContext)
The Raise method has the following object qualifier and named arguments:
Argument |
Description |
object |
Always the Err object. |
Number |
Required. A Long integer that identifies the nature of the error. Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0– 65535. When setting the Number property to your own error code in an OLE Automation object, you add your error code number to the constant vbObjectError. For example, to generate the error number 1050, assign vbObjectError + 1050 to the Number property. (Not all Visual Basic host applications can create OLE Automation objects. See your host application’s documentation to determine whether it can create classes and OLE Automation objects.) |
Source |
Optional. A string expression naming the object or application that originally generated the error. When setting this property for an OLE Automation object, use the form project.class. If nothing is specified, the programmatic ID of the current Visual Basic project is used. |
Description |
Optional. A string expression describing the error. If unspecified, the value in Number is examined. If it can be mapped to a Visual Basic run-time error code, the string that would be returned by the Error function is used as Description. If there is no Visual Basic error corresponding to Number, “Application-defined or object-defined error” is used. |
HelpFile |
Optional. The fully qualified path to the Microsoft Windows Help file in which help on this error can be found. If unspecified, Visual Basic uses the fully qualified drive, path, and filename of the Visual Basic Help file. |
HelpContext |
Optional. The context ID identifying a topic within HelpFile that provides help for the error. If omitted, the Visual Basic Help file context ID for the error corresponding to the Number property is used, if it exists. |
All the arguments are optional except Number. If you use Raise, however, without specifying some arguments, and the property settings of the Err object contain values that have not been cleared, those values serve as the values for your error.
Raise is used for generating run-time errors, and can be used instead of the Error statement (Error still works the same in existing code, and may be appropriate in new code). Raise is useful for generating errors when writing OLE Automation objects, since the Err object gives the programmer and user richer information than is possible if you generate errors with the Error statement. For example, with the Raise method the original generator of the error can be specified in the Source property, online Help for the error can be referenced, and so forth.
Clear Method, Description Property, Err Object, Error Statement, HelpContext Property, HelpFile Property, LastDLLError Property, Number Property, Source Property.
This example uses the Err object’s Raise method to generate an error within an OLE Automation object written in Visual Basic. It has the programmatic ID MyProj.MyObject.
Const MyContextID = 1010407 ' Define a constant for contextID.TestName(CurrentName, NewName) If Instr(NewName, "bob") Then ' Test the validity of NewName. ' Raise the exception. Err.Raise vbObjectError + 27, "MyProj.MyObject", _ "No ""bob"" allowed in your name", "c:\MyProj\MyHelp.Hlp", _ MyContextID End IfFunction