Unconditional Transfer on an Error

The GOTO command instructs GT.M to transfer execution permanently to another line within the routine or to another routine. When stopping to investigate an error is undesirable, use the GOTO command in $ETRAP or $ZTRAP to continue execution at some other point.

Example:

	GTM>ZPRINT ^EP2
	EP2WRITE !,"THIS IS "_$TEXT(+0)
	SET $ECODE="";this affects only $ETRAP
	SET $ETRAP="GOTO ET";this implicitly stacks $ZTRAP
	;N $ZT S $ZT="GOTO ET" ;would give a similar result
	DO SUB1
	WRITE !,"THIS IS THE END"
	QUIT
	SUB1WRITE !,"THIS IS SUB1"
	DO SUB2
	QUIT
	SUB2WRITE !,"THIS IS SUB2"
	KILL A
	BADWRITE A
	WRITE !,"THIS IS NOT DISPLAYED"
	QUIT
	ET;SET $ZTRAP="" ;if using $ZTRAP to prevent recursion
	WRITE !,"CONTINUING WITH ERROR TRAP AFTER AN ERROR"
	WRITE !,"$STACK: ",$STACK
	WRITE !,"$STACK(-1): ",$STACK(-1)
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	FOR I=$STACK(-1):-1:1 DO
	. WRITE !,"LEVEL: ",I
	. SET K=10
	. FOR J="PLACE","MCODE","ECODE" DO
	. . WRITE ?K," ",J,": ",$STACK(I,J)
	. . SET K=K+20
	WRITE !,$ZSTATUS, !
	ZSHOW "S"
	SET $ECODE="";this affects only $ETRAP
	QUIT
	
	GTM>DO ^EP2
	
	THIS IS EP2
	THIS IS SUB1
	THIS IS SUB2
	CONTINUINING WITH ERROR TRAP AFTER AN ERROR
	$STACK: 3
	$STACK(-1): 3
	$ZLEVEL: 4
	LEVEL:3PLACE: BAD^EP2MCODE: BAD WRITE AECODE: ,M6, Z150373850
	LEVEL:2PLACE: SUB1+1^EP2MCODE: DO SUB2ECODE:
	LEVEL:1PLACE: EP2+4^EP2MCODE: DO SUB1ECODE:
	150373850, BAD^EP2,%GTM-E-UNDEF, Undefined local variable: A
	ET+12^EP2
	SUB1+1^EP2
	EP2+4^EP2
	+1^GTM$DMOD(Direct Mode)
	
	THIS IS THE END
	GTM>
	

This routine specifies a GOTO command transferring execution to the ET label when an error occurs. The $ZLEVEL special variable contains an integer indicating the M stack level.

The ZGOTO command is similar to the GOTO command, however, the ZGOTO allows the removal of multiple levels from the program stack. ZGOTO can ensure that execution returns to a specific point, such as a menu.

Example:

	GTM>ZPRINT ^EP3
	EP3 ;
	MENUWRITE !,"THIS IS MENU IN ",$TEXT(0)
	SET $ECODE="";this affects only $ETRAP
	SET $ETRAP="SET $ECODE="""" ZGOTO 2"
	;N $ZT S $ZT="ZGOTO 2" ;would give a similar result
	DO SUB1
	WRITE !,"a??MENUa?? AFTER $ETRAP"
	WRITE !,"$STACK: ",$STACK
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	QUIT
	SUB1WRITE !,"THIS IS SUB1"
	DO SUB2
	WRITE !,"THIS IS SKIPPED BY ZGOTO"
	QUIT
	SUB2WRITE !,"THIS IS SUB2"
	KILL A
	BADWRITE A
	WRITE !,"THIS IS NOT DISPLAYED"
	QUIT
	
	GTM>DO ^EP3
	
	THIS IS MENU IN EP3
	THIS IS SUB1
	THIS IS SUB2
	a??MENUa?? AFTER $ETRAP
	$STACK: 1
	$ZLEVEL: 2
	GTM>	
	

This routine instructs GT.M to reset the execution to level 2 if it encounters an error. GT.M removes all intermediate levels.

In general, coding ZGOTO level information based on $ZLEVEL provides a more robust technique than the "hard-coding" shown in the previous example.

Example:

	GTM> ZPRINT ^EP4
	EP4WRITE !,"THIS IS "_$TEXT(+0)
	SET $ECODE="";this affects only $ETRAP
	DO MAIN
	WRITE !,"THIS IS ",$TEXT(+0)," AFTER THE ERROR"
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	QUIT
	MAINWRITE !,"THIS IS MAIN"
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	SET $ETRAP="ZGOTO "_$ZLEVEL_":ET"
	;N $ZT S $ZT="ZGOTO "_$ZLEVEL_":ET ;alternative
	DO SUB1
	QUIT
	SUB1WRITE !,"THIS IS SUB1"
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	DO SUB2
	QUIT
	SUB2WRITE !,"THIS IS SUB2"
	WRITE !,"$ZLEVEL :",$ZLEVEL
	KILL A
	BADWRITE A
	WRITE !,"THIS IS NOT DISPLAYED"
	QUIT
	ET;SET $ZTRAP="" ;if using $ZTRAP to prevent recursion
	WRITE !,"CONTINUING WITH ERROR TRAP AFTER AN ERROR"
	WRITE !,"$STACK: ",$STACK
	WRITE !,"$STACK(-1): ",$STACK(-1)
	WRITE !,"$ZLEVEL: ",$ZLEVEL
	FOR I=$STACK(-1):-1:1 DO
	. WRITE !,"LEVEL: ",I
	. SET K=10
	. FOR J="PLACE","MCODE","ECODE" DO
	. . WRITE ?K," ",J,": ",$STACK(I,J)
	. . SET K=K+20
	WRITE !,$ZSTATUS,
	ZSHOW "S"
	SET $ECODE="";this affects only $ETRAP
	QUIT
	
	GTM>DO ^EP4
	
	THIS IS EP4
	THIS IS MAIN
	$ZLEVEL: 3
	THIS IS SUB1
	$ZLEVEL: 4
	THIS IS SUB2
	$ZLEVEL: 5
	CONTINUING WITH ERROR TRAP AFTER AN ERROR
	$STACKl 2
	$STACK(-1): 4
	$ZLEVEL: 3
	LEVEL: 4PLACE: BAD^EP4MCODE: BAD WRITE AECODE: ,M6,Z150373850
	LEVEL: 3PLACE: SUB1+2^EP4MCODE: DO SUB2ECODE:
	LEVEL: 2PLACE: MAIN+4^EP4MCODE: DO SUB1ECODE:
	LEVEL: 1PLACE: EP4+2^EP4MCODE: DO MAINECODE:
	150373850,BAD^EP4,%GTM-E-UNDEF, Undefined local variable: A
	ET+12^EP4
	EP4+2^EP4
	+1^GTM$DMOD(Direct mode)
	
	THIS IS EP4 AFTER THE ERROR
	$ZLEVEL: 2
	GTM>	
	

This routine sets $ETRAP or $ZTRAP to a ZGOTO specifying the current level. When the routine encounters an error at label BAD, GT.M switches control to label ET at the level where $ETRAP (or $ZTRAP) was established. At this point in the execution, ET replaces SUB1+2^EP4 as the program stack entry for the level specified, that is, $ZLEVEL=3. The QUIT command then returns control to the level where $ZLEVEL=2.