You use the Raise method of the error object in these situations:
When you want to generate an error on purpose (for example, in testing)
When you want to generate a user-defined error
When no code in the error routine handles the current error, and you want to allow other parts of the call stack to attempt to handle the error
When you want to nest an error handler
Using the Raise method to generate an error on purpose and creating a user-defined error are both complicated and important enough that they require special attention. They are covered in the following sections.
Many times during testing, you want to generate an error so that you can check your own error handling. Instead of figuring out how to cause the error condition, you can use the Raise method of the Err object to accomplish this task, as Listing 16.10 shows.
This code invokes an error 11 (divide by 0).
Another important use of the Raise method of the Err object is the generation of a custom error condition. This is useful when you want to
force an error in response to something that the user did. For example, assume that the user must enter five characters into an unbound text box. Entering only two characters would not generate an Access error. Rather than handling this
user-generated error in some other manner, you can raise the error and have your standard error handler respond to the error condition. Because the Raise method enables you to set all the properties of the Err object, you can create a user-defined error complete with a number, description, source, and so on, as shown in Listing 16.11.
Although it is very simple, Creating a Generic Error Handler," later in this chapter, explores how to put together a generic error handler. By passing user-defined errors through your generic error handler, all errorsuser-defined or notare handled in the same way.