VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 11

Methods of the Err Object

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

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.

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.

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).

The Raise method is usually used within class modules. It allows you to generate your own runtime errors to pass information to another application using your application as a server. The Raise method is discussed further in the section titled "Sending Errors from a COM Component" in Chapter 12.


<< Previous | Contents | Next >>


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved