WORDPERFECT MACRO TUTORIAL


16. Error Handling

16.1 Trapping and handling errors

If execution of a macro causes an error in WordPerfect (e.g., if the macro attempts to open a file that does not exist), or the macro executes a search that returns a "not found," the macro will end with the error or not-found message box displayed on the screen. This looks ugly and can be disconcerting to the user, so you might want to prevent it. Or you may want to have the macro perform alternative operations, depending on whether a not-found or error condition was encountered or not. To handle these situations you can "trap" these conditions and have the macro perform some other action (rather than having the system display the usual message boxes and terminate the macro).

16.2 The OnNotFound() and OnError() commands

The commands OnNotFound() and OnError are used to have the macro perform certain actions when a not-found or error condition is encountered, respectively. Specifically, they instruct the macro to jump to a label in the macro as soon as the not-found or error condition is encountered, and resume execution at that point. (This is similar to the operation of the Go() command.) These error-trapping commands must be executed before the command that would cause the not-found or error condition to occur. For example, you would put the OnNotFound command before the search command to which it applies.

The form of these commands are:

OnNotFound([labelname])

OnError([labelname])

where the label name identifies the point in the macro that you want to jump to when the condition occurs; that point is marked with a Label() with that name.

An OnNotFound() command remains in effect in a macro until another OnNotFound() command is executed (with an instruction to jump to a different label). The same is true for the OnError() command. If no labelname is specified, error and not-found conditions will be processed as they normally would be by WordPerfect, so the commands OnNotFound() and OnError() will "reset" the program behavior for these conditions to the default.

One place where you might want to use the OnNotFound() command is in a macro that performs a search repeatedly throughout the document, performing the same action each time it finds the search string (i.e., in a loop). This is often necessary to make repeated identical changes that cannot be accomplished by a global replace. Eventually the search will fail because no further occurrences of the search string exist, and normally the ugly "not found" box would be displayed.

To avoid this, you can just put a label at the very end of the macro, and insert a OnNotFound() command to jump to that label when the search fails. Since that Label() is the last command in the macro, the macro simply ends, without a "not found" box displayed. Of course, you need not end the macro at that point; you could insert commands for the macro to continue to do additional processing.

Another occasion to use error-trapping is when you want the macro to perform two different operations, depending on whether some text exists or not. The macro code for this would be:

OnNotFound(NotThere@)
SearchString([string to look for])
SearchNext()
[commands to be executed if text exists]
Label(NotThere@)
[commands to be executed if text does not exists]

An alternative method of handling not-found conditions is to use the NotFound() command, and/or the ?NotFound system variable. These commands are beyond the scope of this tutorial.




END OF TUTORIAL


© 1999 Seth H. Katz
All rights reserved


E-MAIL TO AUTHOR


BACK Table of
Contents