Try OpenEdge Now
skip to main content
Error Handling
Traditional Error Handling : Specifying error handling behavior : Using labels
 

Using labels

The example that follows sets up a common set of nested FOR EACH blocks that list the order numbers for the first few customer records in the Sports2000 database. Within the inner block, a nonsensical FIND statement raises error after the first iteration. This trivial framework allows you to test the interactions of ON ERROR phrases.
This version demonstrates how a label can be used to undo the transaction associated with the outer block rather than just the subtransaction of the inner block, as shown:
PROCEDURE NestedBlocks:

Outer-Block:
    FOR EACH Customer WHERE CustNum < 5:
ASSIGN Customer.Name = Customer.Name + "_changed".

Inner-Block:
        FOR EACH Order OF Customer
ON ERROR UNDO Outer-Block, RETURN:

DISPLAY OrderNum.

/* Nonsense code raises ERROR. */
FIND SalesRep WHERE RepName = Customer.Name.

END. /* Inner-Block */
    END. /* Outer-Block */

DISPLAY "For Blocks Complete".
END PROCEDURE.

RUN NestedBlocks.

DISPLAY "Procedure NestedBlocks Complete."
The flow of this example is as follows:
*The ASSIGN statement in Outer-Block starts a transaction
*The FIND statement in Inner-Block raises the ERROR condition
*Inner-Block is automatically undone
*The explicit ON ERROR phrase of Inner-Block activates
*Outer-Block is undone
*Control returns to the caller, which is the main procedure block
The following table lists all the ON ERROR phrases in effect in this procedure from the outermost to the innermost.
Table 4. ON ERROR phrases
Block
ON ERROR phrase
Procedure block (.p file)
Implicit ON ERROR UNDO, LEAVE
Internal procedure NestedBlocks
Implicit ON ERROR UNDO, LEAVE
FOR EACH block labeled Outer-Block
Implicit ON ERROR UNDO, NEXT
FOR EACH block labeled Inner-Block
Explicit ON ERROR UNDO Outer-Block, RETURN
When the AVM raises ERROR on the first iteration of Inner-Block, the explicit ON ERROR phrases directs the AVM to undo Outer-Block and use the RETURN branching option. RETURN anywhere in a routine forces control back to the caller, which is the procedure file. Since the RETURN option did not include the ERROR option, ERROR is not raised in the procedure block, and the final DISPLAY statement executes.
If you change the explicit ON ERROR phrase as shown in the following code snippet, you will see almost identical behavior, but the final display statement will not execute:
Inner-Block:
FOR EACH Order OF Customer
    ON ERROR UNDO Outer-Block, RETURN ERROR:
Raising ERROR in the procedure block returns control to the OpenEdge Editor.
If you change the explicit ON ERROR phrase to use the NEXT branching option, for each Customer record you will see one Order number displayed followed by an error message:
Inner-Block:
FOR EACH Order OF Customer
    ON ERROR UNDO Outer-Block, NEXT:
Finally, if you delete the explicit ON ERROR phrase, the implicit ON ERROR phrase for Inner-Block will be ON ERROR UNDO, NEXT. The UNDO operation will be applied to Inner-Block instead of Outer-Block. You will see one error message for each iteration of Inner-Block for each Customer record.