Setting $ZTRAP for Each Level

The command NEW $ETRAP or NEW $ZTRAP stacks the current value of $ETRAP or $ZTRAP respectively and sets the value equal to the empty string. Normally, a SET $ETRAP or $ZTRAP immediately follows a NEW $ETRAP or $ZTRAP. When GT.M encounters a QUIT command that leaves a level where $ETRAP or $ZTRAP had been NEWed, GT.M deletes any value set to the ISV after the NEW command and restores the value that the ISV held previous to the NEW. NEW $ETRAP or $ZTRAP enables the construction of error handlers corresponding to the nesting of routines. A SET $ETRAP or $ZTRAP implcitily NEWs the other variable if it does not already have the value of the empty string. This enables the interleaving of $ETRAP and $ZTRAP at different levels, although (as mentioned above) such interleaving requires that $ZTRAP handlers deal appropriately with $ECODE.

Example:

	GTM>ZPRINT ^EP5
	EP5WRITE !,"THIS IS "_$TEXT(+0)
	SET $ECODE="";this affects only $ETRAP
	WRITE !,"STARTING $ETRAP: ",$ETRAP
	WRITE !,"STARTING $ZTRAP: ",$ZTRAP
	DO SUB1
	WRITE !,"ENDING $ETRAP: ",$ETRAP
	WRITE !,"ENDING $ZTRAP: ",$ZTRAP
	QUIT
	MAINWRITE !,"THIS IS MAIN"
	WRITE !,$ZLEVEL: ",$ZLEVEL
	DO SUB1
	QUIT
	SUB1WRITE !,"THIS IS SUB1"
	NEW $ETRAP SET $ETRAP="GOTO ET1"
	;NEW $ZTRAP SET $ZTRAP="GOTO ET1" ;alternative
	WRITE !,"$ETRAP FOR SUB1: ",$ETRAP
	KILL A
	BADWRITE A
	WRITE !,"THIS IS NOT DISPLAYED"
	QUIT
	ET1WRITE !,"ERROR TRAP 1"
	WRITE !,"$ETRAP AFTER THE TRAP: ",$ETRAP
	WRITE !,"$ZTRAP AFTER THE TRAP: ",$ZTRAP
	SET $ECODE="";this affects only $ETRAP
	QUIT
	
	GTM>DO ^EP5
	
	THIS IS EP5
	STARTING $ETRAP:
	STARTING $ZTRAP: B
	THIS IS SUB1
	$ETRAP FOR SUB1: GOTO ET1
	ERROR TRAP 1
	$ETRAP AFTER THE TRAP: GOTO ET1
	$ZTRAP AFTER THE TRAP:
	ENDING $ETRAP:
	ENDING $ZTRAP: B
	GTM>	
	

At SUB1, this routine NEWs $ETRAP and assigns it a value, which implicitly NEWs $ZTRAP. When the routine encounters an error at the SUB1 level, GT.M transfers control to label ET1 without modifying the value of $ETRAP or $ZTRAP. When the routine encounters a QUIT command in routine ET1, GT.M transfers control to the command after the DO that invoked ET1 and restores $ETRAP or $ZTRAP to the values they held before the NEW and the SET.

[Note]

If the transfer to ET1 was accomplished with a ZGOTO that reduced the stack level, after the trap, $ETRAP would have the value of the empty string and $ZTRAP would be "B".