Error Handling in APL

I am currently working on an APL program for a class and am facing an error handling problem.

In the function I made, I want to check that the input is integer. If this is not the case, I want to return an error message and not run the rest of the function. So far I have been comparing this if it were equal to the sex itself. If not, I do not want the function to start and need to be stopped. It works if I put 4.2and gives an error message, but does not work if I put something like 'A'in or 'ABCDEF'and just gives a normal error. I tried to make a try catch statement, but it gave me an error when it got :Tryin my function.

This is not what I want. How can I make the function end with an error message instead of continuing if the input character or string? I know that I can put all the code in an if block, but this seems really unnecessary.

My code is in text format:

 TESTER Q;error
 :If Q≢⌊Q
     'Possible'
 :Else
     'Not Possible'
 :EndIf
 'Again, Possible'

And as a screenshot:

Screen shot

+4
source share
1 answer

If you want to explicitly leave early in order to avoid including all the code in the block :If, you can do something like:

 r←TESTER Q
 :If 0≢⊃0⍴⊂Q ⍝ Q not a simple scalar number
 :OrIf Q≢⌊Q  ⍝ Q not an integer
     r←'Not Possible'0
 :EndIf
 r←'Possible'

This works using APL prototypes:

⊂Q Q .
0⍴ . , Q, , , , . , Q , 0, .

!

, , , ( ), , , . :

 r←TESTER Q
 :If 0≢⊃0⍴⊂Q ⍝ Q not a simple scalar number
 :OrIf Q≢⌊Q  ⍝ Q not an integer
     'Not Possible'⎕SIGNAL 11
 :EndIf
 r←'Possible'

!

⎕SIGNAL ( ) , - . 11 - DOMAIN ERROR, .


, :Try, . - , Dyalog APL, :

 :Trap 4 5 6 10 11 16
     code to try goes here
 :CaseList 4 5
     handling of rank and length errors go here
 :Case 6
     handling of value errors goes here
 :Else
     all other trapped errors are handled here
 :EndTrap
 untrapped errors will throw as usual

, . (:Try - , APLX.)

+3

Source: https://habr.com/ru/post/1694617/


All Articles