Note: The use of
The system function
∇FOO;Z  Z←⎕ERX LABEL Z will contain the previous value of ⎕ERX
Control will pass to LABEL when an error occurs in this function, (or a called function which does not have error trapping set).
A non-error trapped function, called by an error trapped function, will behave as if it is locked. A branch will again take place to the designated line in the calling function.
Having transferred execution to an error handling routine, it is important to know the type of error that has occurred and also where it occurred. Sometimes the APL function can attempt some sort of corrective action, but often the error is logged and some message passed to the user.
To read the error message, the system function
∇FOO;Z;ER  Z←⎕ERX ERR  100×'A'  'THIS LINE WILL NOT BE REACHED'  ERR: 'INTERNAL PROGRAM ERROR'  ER←⎕R ⎕BOX ⎕ERM ⍝ FORM THE ⎕ERM VECTOR INTO A MATRIX  'ERROR MESSAGE: ',(ER[1;]),' ON FUNCTION LINE ',⍕⎕LER ∇
Example of Error Trapping
It is possible to encounter the error message WS FULL if you try to carry out operations using large arrays. Rather than have your function stop, you might like to check for this error state and undertake corrective action.
∇ADDUP;X;DATA  ⍝ADDS UP ALL THE NUMBERS UP TO THAT ENTERED  X←⎕ERX ERR ⍝ X IS USED TO HIDE THE RETURN FROM ⎕ERX  START:'ENTER A NUMBER'  DATA←⌊⎕ ⍝ MAKE IT THE NEXT LOWEST INTEGER  'THE SUM OF THE FIRST ',(⍕DATA),' NUMBERS IS: '  +/⍳DATA  →0 ⍝ END OF THE FUNCTION  ERR:→(1≠1↑⎕LER)/REALERR ⍝ ERR CODE 1 IS WS FULL  'THE NUMBER YOU ENTERED WAS TOO BIG TO USE, TRY AGAIN'  →START  REALERR: ⍝ AN ERROR HAS OCCURRED WHICH IS NOT WS FULL  'ERROR TYPE ',(⍕1↑⎕LER),' ON LINE ',⍕1↓⎕LER  'MESSAGE IS:'  ⎕ERM  ∇
Make sure that you have some escape route from the error trap routine, otherwise any error within that section of the function will cause an uninterruptible loop. (The Interrupt key or menu item also causes an error
If some error report is to be made to the user of the system, it is useful to be able to modify the usual APL error messages, which may not be very meaningful to the end user. This can be carried out by the function
Here is an example where
∇FOO;DATA  ⎕ERX ERR ⍝ SET ERROR TRAP  L:'ENTER YOUR EXPRESSION'  DATA←⍞  'THE RESULT IS:'  ⍎ DATA  →L  ERR: →(13≠1↑⎕LER)/NOTINT⍝ NOT INTERRUPT  'DID YOU MEAN TO HIT INTERRUPT? (Y/N)'  →('Y'≠1↑⍞)/L ⋄ ⎕ERS 13 ⍝ SIGNAL IF CONFIRMED  NOTINT: ⎕ERS 1↑⎕LER ⍝ SIGNAL OTHER ERRORS ∇
If the right argument is a number in the range 1 to 51,
∇R←AV B  'NUMERIC ARGUMENT PLEASE' ⎕ERS (4=⎕DR B)/8  R←(+/B)÷⍴B ∇
AV 1 2 3 2 AV 'ABC' NUMERIC ARGUMENT PLEASE AV ^ ⎕LER 8 0 ⎕SI (empty response) (the function has been halted)
Copyright © 1996-2010 MicroAPL Ltd