$ETRAP or $ZTRAP set to a DO command instructs GT.M to transfer execution temporarily to another line within this or another routine when it encounters an error. A QUIT command within the scope of the DO transfers control back to the code specified by the $ETRAP or $ZTRAP. When the code in the ISV terminates due to an explicit or implicit QUIT, the behavior of $ETRAP and $ZTRAP is different. When $ETRAP is in control, the level at which the error occurred is removed, and control returns to the invoking level. When $ZTRAP contains code, execution picks up at the beginning of the line with the error. A DO command within $ZTRAP is normally used for I/O errors that an operator may resolve, because a DO command permits re-execution of the line containing the error.
Example:
GTM>ZPRINT ^EP6 EP6WRITE !,"THIS IS "_$TEXT(+0) NEW NEW $ZTRAP SET $ZTRAP="DO ET" SET (CB,CE)=0 BADSET CB=CB+1 WRITE A SET CE=CE+1 WRITE !,"AFTER SUCCESSFUL EXECUTION OF BAD:",! ZWRITE SET 1="A IS NOT DEFINED" QUIT GTM>DO ^EP6 THIS IS EP6 CONTINUING WITH ERROR TRAP AFTER AN ERROR CB=1 CE-0 A IS NOT DEFINED AFTER SUCCESSFUL EXECUTION OF BAD: A="A IS NOW DEFINED" CB=2 CE=1
This example sets $ZTRAP to a DO command. When the routine encounters an error in the middle of the line at label BAD, GT.M transfers control to label ET. After QUITting from routine ET, GT.M returns control to the beginning of the ine at label BAD.
Example:
GTM>ZPRINT ^EP6A EP6AWRITE !,"THIS IS "_$TEXT(+0) NEW NEW $ETRAP SET $ETRAP="GOTO ET" SET (CB,CE)=0 BADSET CB=CB+1 WRITE A SET CE=CE+1 WRITE !,"AFTER SUCCESSFUL EXECUTION OF BAD:",! ZWRITE QUIT ETW !,"CONTINUING WITH ERROR TRAP AFTER AN ERROR",! ZWRITE SET A="A IS NOW DEFINED" SET RETRY=$STACK($STACK,"PLACE") SET $ECODE="" GOTO @RETRY GTM>DO EP6A THIS IS EP6A CONTINUING WITH ERROR TRAP AFTER AN ERROR CB=1 CE=0 A IS NOW DEFINED AFTER SUCCESSFUL EXECUTION OF BAD: A="A IS NOW DEFINED" CB=2 CE=1 RETRY="BAD^EP6A" GTM>
This routine is an example of how $ETRAP handling can be coded to perform the same kind of resumtion of the original execution stream that occurs by default with $ZTRAP when there is no unconditional transfer of control.