Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

$STACK

M[UMPS] by Example

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
 Write !,"NOTE: The error trap should have ended here."
 Write !,"Now let’s generate a user-defined error:"
 Set $ECode=",User defined error,"
 Write !,"Now leaving subroutine L1, $STACK=",$STack
 Write ", $ESTACK=",$EStack
 Quit
L2 New $EStack,$ETrap
 Write !,"Now at label L2, $STACK=",$STack
 Write ", $ESTACK=",$EStack
 Set $ETrap="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" Write !?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
Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

Copyright © Standard Documents; 1977-2024 MUMPS Development Committee;
Copyright © Examples: 1995-2024 Ed de Moel;
Copyright © Annotations: 2003-2008 Jacquard Systems Research
Copyright © Annotations: 2008-2024 Ed de Moel.

The information in this page is NOT authoritative and subject to be modified at any moment.
Please consult the appropriate (draft) language standard for an authoritative definition.

Some specifications are "approved for inclusion in a future standard". Note that the MUMPS Development Committee cannot guarantee that such future standards will indeed be published.

This page most recently updated on 15-Nov-2023, 13:13:25.

For comments, contact Ed de Moel (demoel@jacquardsystems.com)