Examples using ON ERROR
- Last Updated: March 30, 2020
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
The following simple code sample illustrates some of the ON phrase constructs.
|
The following table lists all the ON ERROR phrases in effect in this
procedure from the outermost to the innermost.
| Block | ON ERROR phrase | ||
| Procedure block (.p file) | Implicit ON ERROR UNDO, LEAVE |
||
Internal procedure ScanCustomers |
Implicit ON ERROR UNDO, LEAVE |
||
FOR EACH Customer block |
Implicit ON ERROR UNDO, NEXT |
||
FOR EACH Order block |
Explicit ON ERROR UNDO, RETURN |
||
When the AVM raises ERROR in the FOR EACH ORDER block, the explicit ON ERROR phrase directs the AVM to return to the caller which is the
procedure file. Since the RETURN option does not
include the ERROR option, ERROR is not raised in the procedure block, and the final DISPLAY statement executes. However, the first DISPLAY statement (“For blocks complete”) does not run.
If you change the explicit ON ERROR phrase as
shown in the following code snippet, you see almost identical behavior, except the final
display statement does not execute:
|
Due to the ON phrase shown, ERROR is then raised in the procedure block. The AVM then
executes the default LEAVE action and return control to
its caller. If this is the top-level procedure, the application ends. If you change the
explicit ON ERROR phrase as shown in the following code
snippet, an error object is created and raised in the outer block, which is the FOR EACH Customer block:
|
Since there is no explicit ON phrase, the
default action occurs, which is that the error message is displayed and the AVM goes to
the next Customer iteration.
If you remove the explicit ON ERROR phrase
altogether, the implicit ON ERROR phrase is ON ERROR UNDO, NEXT, and one error message is displayed for
each Order of each Customer record.