Simulating Run-Time Errors

Simulating run-time errors is useful when you're testing your applications or when you want to treat a particular condition as a run-time error. For example, you might write a module that calls routines in a dynamic-link library (DLL) and want the rest of your application to handle the DLL's return values as actual Visual Basic errors — that is, to generate run-time errors in Microsoft Excel.

You can simulate any Visual Basic run-time error by supplying the error code for that error in the Error statement, as in the following example.


Error 71        ' Simulate "Disk Not Ready" error.

You can also use the Error statement to generate your own, user-defined errors by supplying an error code that doesn't correspond to a Visual Basic run-time error. Of course, this is useful only if you also write code that handles the new errors you define. For a list of built-in errors, see "Trappable errors" in Help.

Note

As new errors are defined in future versions of Visual Basic, more error numbers will be assigned to built-in errors. If you want to generate and trap your own errors, begin your numbering scheme with 50,000 and work upward from there. This way, you'll avoid using the same number for your own error that a future version of Visual Basic might use for a built-in error.