M[UMPS] Functions - $ST[ACK]

Introduced in the 1995 ANSI M[UMPS] language standard.

This function returns information extracted from the error-trap status information stack.

Assume that a routine called T4EDM exists, with the following code:

T4EDM ; test error trapping
X WRITE !,"Start test routine, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 SET $ECODE="",SHOWN=0
 DO L1
 WRITE !,"At end of routine, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 QUIT
L1 WRITE !,"Now at label L1, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 SET $ETRAP="w !,""Error trap from L1 ("",$STACK,""/"",$ESTACK,"")."" DO E1 DO SHOW($STACK)"
 DO L2
 SET SHOWN=0
 w !,"NOTE: The error trap should have ended here."
 w !,"Now let's generate a user-defined error:"
 SET $ECODE=",User defined error,"
 w !,"Now leaving subroutine L1, $STACK=",$STACK
 w ", $ESTACK=",$ESTACK
 QUIT
L2 NEW $ESTACK,$ETRAP
 WRITE !,"Now at label L2, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 s $et="w !,""Error trap from L2 ("",$STACK,""/"",$ESTACK,"")."" do E2 s:'$ESTACK $ECODE="""" DO SHOW($STACK)"
 DO L3
 WRITE !,"Now leaving subroutine L2, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 QUIT
L3 WRITE !,"Now at label L3, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 SET X="w !,""Now comes the error:"" set A=1/0"
 SET Y="xecute X"
 SET A="x Y"
 XECUTE A
 WRITE !,"Now leaving subroutine L3, $STACK=",$STACK
 WRITE ", $ESTACK=",$ESTACK
 QUIT
E1 WRITE " E1 ",$ECODE,! DO TRACE QUIT
E2 WRITE " E2 ",$ECODE,! DO TRACE QUIT
TRACE NEW MAX,I,K,HERE
 QUIT:SHOWN
 SET MAX=$STACK(-1),HERE=$STACK
 FOR K=0:1:MAX DO
 . WRITE !,$SELECT(HERE=K:" * ",1:"   "),$JUSTIFY(K,2),": ",$STACK(K)
 . FOR I="PLACE","MCODE","ECODE" W !?9,$STACK(K,I)
 . QUIT
 SET SHOWN=1
 QUIT
SHOW(N) WRITE !,"In show, N=",N,",$STACK=",$STACK
 QUIT

Execution of this routine will yield the following result:

do X

Start test routine, $STACK=1, $ESTACK=1
Now at label L1, $STACK=2, $ESTACK=2
Now at label L2, $STACK=3, $ESTACK=0
Now at label L3, $STACK=4, $ESTACK=1
Now comes the error:
Error trap from L2 (7/4). E2 ,M9,

    0:
          @ +1
          do X

    1: DO
          X+3^T4EDM +1
           DO L1

    2: DO
          L1+3^T4EDM +1
           DO L2

    3: DO
          L2+4^T4EDM +1
           DO L3

    4: DO
          L3+5^T4EDM +1
           XECUTE A

    5: XECUTE
          @ +1
          x Y

    6: XECUTE
          @ +1
          xecute X

    7: XECUTE
          @ +2
          w !,"Now comes the error:" set A=1/0
          ,M9,
    8: DO
          E2^T4EDM +2
          E2 WRITE " E2 ",$ECODE,! DO TRACE QUIT

 *  9: DO
          TRACE+3^T4EDM +2
           FOR K=0:1:MAX DO

In show, N=7, $STACK=8
Error trap from L2 (6/3). E2 ,M9,

In show, N=6, $STACK=7
Error trap from L2 (5/2). E2 ,M9,

In show, N=5, $STACK=6
Error trap from L2 (4/1). E2 ,M9,

In show, N=4, $STACK=5
Error trap from L2 (3/0). E2 ,M9,

In show, N=3, $STACK=4
NOTE: The error trap should have ended here.
Now let's generate a user-defined error:
Error trap from L1 (2/2). e1 ,User defined error,.

    0:
          @ +1
          do X

    1: DO
          X+3^T4EDM +1
           DO L1

    2: DO
          L1+7^T4EDM +1
           SET $ECODE=",User defined error,"
          ,User defined error,
    3: DO
          E1^T4EDM +2
          E1 WRITE " E1 ",$ECODE,! DO TRACE QUIT

 *  4: DO
          TRACE+3^T4EDM +2
           FOR K=0:1:MAX DO

In show, N=2, $STACK=3
Error trap from L1 (1/1). E1 ,User defined error,

In show, N=1, $STACK=2
Error trap from L1 (0/0). E1 ,User defined error,

In show, N=0, $STACK=1

Now, let s have a look at the stacking of variables and error-traps.
Assume that a routine called T1EDM exists, with the following code:

T1EDM ;
 WRITE "Start: $STACK = ",$STACK
 WRITE ", $ECODE = """,$ECODE,""","
 WRITE !?5,"$ETRAP = """,$ETRAP
 WRITE """, $ESTACK = ",$ESTACK,!
 SET LEVEL=1
 SET $ETRAP="GOTO ERROR"
 DO X
 QUIT
 ;
X IF LEVEL#3=0 NEW $ESTACK
 IF LEVEL#4=0 NEW LOCAL SET LOCAL(LEVEL)=LEVEL
 SET LOCAL(LEVEL,LEVEL)="Demo"
 IF LEVEL=16 NEW $ETRAP SET $ETRAP="GOTO NESTERR"
 WRITE !,"Level = ",LEVEL
 WRITE ", $STACK = ",$STACK
 WRITE ", $ECODE = """,$ECODE,""","
 WRITE !?5,"$ETRAP = """,$ETRAP
 WRITE """, $ESTACK = ",$ESTACK,!
 SET LEVEL=LEVEL+1
 IF LEVEL<20 DO X
 ELSE  SET $ECODE=",U13," ; User defined error code
 WRITE !!,"Back: level = ",LEVEL
 WRITE ", $STACK = ",$STACK
 WRITE ", $ECODE = """,$ECODE,""","
 WRITE !?5,"$ETRAP = """,$ETRAP
 WRITE """, $ESTACK = ",$ESTACK,!
 QUIT
 ;
ERROR WRITE !!,"Error: level = ",LEVEL
 WRITE ", $STACK = ",$STACK
 WRITE ", $ECODE = """,$ECODE,""","
 WRITE !?5,"$ETRAP = """,$ETRAP
 WRITE """, $ESTACK = ",$ESTACK
 WRITE !,"Error location: """
 WRITE $STACK($STACK,"PLACE"),"""."
 WRITE !,"MUMPS code: """
 WRITE $STACK($STACK,"MCODE"),"""."
 WRITE !,"Error code: """
 WRITE $STACK($STACK,"ECODE"),"""."
 SET R="LOCAL"
 FOR  SET R=$QUERY(@R) QUIT:R=""  DO
 . WRITE !,R," = ",@R
 . QUIT
 ;
 IF $STACK<7 SET $ECODE=""
 ;
 ; As long as the value of $ECODE is
 ; non-empty, the system will execute
 ; the error trap upon exit from the
 ; error trap routine.
 QUIT
 ;
NESTERR ;
 WRITE !,"Nested error trap"
 WRITE ", $STACK = ",$STACK
 QUIT

Then the command DO ^T1EDM will produce the following result:

Start: $STACK = 1, $ECODE = "",
     $ETRAP = "", $ESTACK = 1

Level = 1, $STACK = 2, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

Level = 2, $STACK = 3, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 3

Level = 3, $STACK = 4, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0

Level = 4, $STACK = 5, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 1

Level = 5, $STACK = 6, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

Level = 6, $STACK = 7, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0

Level = 7, $STACK = 8, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 1

Level = 8, $STACK = 9, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

Level = 9, $STACK = 10, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0

Level = 10, $STACK = 11, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 1

Level = 11, $STACK = 12, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

Level = 12, $STACK = 13, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0

Level = 13, $STACK = 14, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 1

Level = 14, $STACK = 15, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

Level = 15, $STACK = 16, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0

Level = 16, $STACK = 17, $ECODE = "",
     $ETRAP = "GOTO NESTERR", $ESTACK = 1

Level = 17, $STACK = 18, $ECODE = "",
     $ETRAP = "GOTO NESTERR", $ESTACK = 2

Level = 18, $STACK = 19, $ECODE = "",
     $ETRAP = "GOTO NESTERR", $ESTACK = 0

Level = 19, $STACK = 20, $ECODE = "",
     $ETRAP = "GOTO NESTERR", $ESTACK = 1

Nested error trap, $STACK = 20
Nested error trap, $STACK = 19
Nested error trap, $STACK = 18
Nested error trap, $STACK = 17

Error: level = 20, $STACK = 16, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 0
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(12) = 12
LOCAL(12,12) = Demo
LOCAL(13,13) = Demo
LOCAL(14,14) = Demo
LOCAL(15,15) = Demo

Error: level = 20, $STACK = 15, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 2
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(12) = 12
LOCAL(12,12) = Demo
LOCAL(13,13) = Demo
LOCAL(14,14) = Demo
LOCAL(15,15) = Demo

Error: level = 20, $STACK = 14, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 1
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(12) = 12
LOCAL(12,12) = Demo
LOCAL(13,13) = Demo
LOCAL(14,14) = Demo
LOCAL(15,15) = Demo

Error: level = 20, $STACK = 13, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 0
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(12) = 12
LOCAL(12,12) = Demo
LOCAL(13,13) = Demo
LOCAL(14,14) = Demo
LOCAL(15,15) = Demo

Error: level = 20, $STACK = 12, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 2
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(8) = 8
LOCAL(8,8) = Demo
LOCAL(9,9) = Demo
LOCAL(10,10) = Demo
LOCAL(11,11) = Demo

Error: level = 20, $STACK = 11, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 1
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(8) = 8
LOCAL(8,8) = Demo
LOCAL(9,9) = Demo
LOCAL(10,10) = Demo
LOCAL(11,11) = Demo

Error: level = 20, $STACK = 10, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 0
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(8) = 8
LOCAL(8,8) = Demo
LOCAL(9,9) = Demo
LOCAL(10,10) = Demo
LOCAL(11,11) = Demo

Error: level = 20, $STACK = 9, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 2
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(8) = 8
LOCAL(8,8) = Demo
LOCAL(9,9) = Demo
LOCAL(10,10) = Demo
LOCAL(11,11) = Demo

Error: level = 20, $STACK = 8, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 1
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(4) = 4
LOCAL(4,4) = Demo
LOCAL(5,5) = Demo
LOCAL(6,6) = Demo
LOCAL(7,7) = Demo

Error: level = 20, $STACK = 7, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 0
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(4) = 4
LOCAL(4,4) = Demo
LOCAL(5,5) = Demo
LOCAL(6,6) = Demo
LOCAL(7,7) = Demo

Error: level = 20, $STACK = 6, $ECODE = ",U13,",
     $ETRAP = "GOTO ERROR", $ESTACK = 2
Error location: "X+10^T1EDM +2".
MUMPS code: " IF LEVEL<20 D X".
Error code: "".
LOCAL(4) = 4
LOCAL(4,4) = Demo
LOCAL(5,5) = Demo
LOCAL(6,6) = Demo
LOCAL(7,7) = Demo

Back: level = 20, $STACK = 5, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 1


Back: level = 20, $STACK = 4, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 0


Back: level = 20, $STACK = 3, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 3


Back: level = 20, $STACK = 2, $ECODE = "",
     $ETRAP = "GOTO ERROR", $ESTACK = 2

This document is © Ed de Moel, 1995-2005.
It is part of a book by Ed de Moel that is published under the title "M[UMPS] by Example" (ISBN 0-918118-42-5).
Printed copies of the book are no longer available.