APLX Help : Help on APL language : Errors : Error Trapping (⎕ERX)
|
|
![]() |
Error Trapping
|
Note: The use of The system function ∇FOO;Z [1] 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. The function To read the error message, the system function ∇FOO;Z;ER [1] Z←⎕ERX ERR [2] 100×'A' [3] 'THIS LINE WILL NOT BE REACHED' [4] ERR: 'INTERNAL PROGRAM ERROR' [5] ER←⎕R ⎕BOX ⎕ERM ⍝ FORM THE ⎕ERM VECTOR INTO A MATRIX [6] 'ERROR MESSAGE: ',(ER[1;]),' ON FUNCTION LINE ',⍕⎕LER[2] ∇ Example of Error TrappingIt 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 [1] ⍝ADDS UP ALL THE NUMBERS UP TO THAT ENTERED [2] X←⎕ERX ERR ⍝ X IS USED TO HIDE THE RETURN FROM ⎕ERX [3] START:'ENTER A NUMBER' [4] DATA←⌊⎕ ⍝ MAKE IT THE NEXT LOWEST INTEGER [5] 'THE SUM OF THE FIRST ',(⍕DATA),' NUMBERS IS: ' [6] +/⍳DATA [7] →0 ⍝ END OF THE FUNCTION [8] ERR:→(1≠1↑⎕LER)/REALERR ⍝ ERR CODE 1 IS WS FULL [9] 'THE NUMBER YOU ENTERED WAS TOO BIG TO USE, TRY AGAIN' [10] →START [11] REALERR: ⍝ AN ERROR HAS OCCURRED WHICH IS NOT WS FULL [12] 'ERROR TYPE ',(⍕1↑⎕LER),' ON LINE ',⍕1↓⎕LER [13] 'MESSAGE IS:' [14] ⎕ERM [15] ∇ 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 Error SignallingIf 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 [1] ⎕ERX ERR ⍝ SET ERROR TRAP [2] L:'ENTER YOUR EXPRESSION' [3] DATA←⍞ [4] 'THE RESULT IS:' [5] ⍎ DATA [6] →L [7] ERR: →(13≠1↑⎕LER)/NOTINT⍝ NOT INTERRUPT [8] 'DID YOU MEAN TO HIT INTERRUPT? (Y/N)' [9] →('Y'≠1↑⍞)/L ⋄ ⎕ERS 13 ⍝ SIGNAL IF CONFIRMED [10] NOTINT: ⎕ERS 1↑⎕LER ⍝ SIGNAL OTHER ERRORS ∇ If the right argument is a number in the range 1 to 51, ∇R←AV B [1] 'NUMERIC ARGUMENT PLEASE' ⎕ERS (4=⎕DR B)/8 [2] 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) |
|
APLX Help : Help on APL language : Errors : Error Trapping (⎕ERX)
|
Copyright © 1996-2010 MicroAPL Ltd