APL中的错误处理 [英] Error Handling in APL
问题描述
我目前正在为一个类编写APL程序,并且遇到了错误处理问题.
I am currently working on an APL program for a class and have run into an issue with error handling.
在我做的函数中,我想检查一下输入是否为整数.如果不是,我想返回一条错误消息,而不运行其余功能.到目前为止,我进行比较以查看它是否等于其底数.如果没有,我不希望该函数运行并希望它停止.如果我放入4.2
并给出错误消息,它可以工作,但是如果我放入'A'
或'ABCDEF'
之类的东西并给出正常错误,则不起作用.我尝试制作try catch语句,但是在我的函数中到达:Try
时,它给了我一个错误.
In a function I made, I want to check to see that the input is an integer. If it is not, I want to return an error message and not run the rest of the function. So far, I compare to see if it's equal to the floor of itself. If not, I don't want the function to run and want it to stop. It works if I put 4.2
and gives an error message, but doesn't work if I put something like 'A'
in or 'ABCDEF'
and just gives a normal error. I tried making a try catch statement, but it gave me an error when it got to :Try
in my function.
这不是我想要的.如何使函数以错误消息结尾而不是在输入是字符还是字符串的情况下继续呢?我知道我可以将整个代码放在if块中,但这似乎真的没有必要.
Which isn't what I want. How can I make it so the function ends with an error message instead of continuing if the input is a character or string? I know I could put the entire code in an if block, but that seems really unnecessary.
我的纯文本代码
TESTER Q;error
:If Q≢⌊Q
'Possible'
:Else
'Not Possible'
:EndIf
'Again, Possible'
并作为屏幕截图:
推荐答案
如果您想提早退出,以避免将整个代码包含在:If
块中,则可以执行以下操作:
If you want to explicitly quit early to avoid enclosing the entire code in a :If
block, you could 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'
这可以通过使用APL的原型来实现:
This works by using APL's prototypes:
⊂Q
确保确保整体处理Q
.
0⍴
列出该类型"的空列表.
⊃
强制出原型元素,就像Q
一样,但所有字符都转换为空格,所有数字都转换为零.现在,如果Q
是一个简单的标量数字,则原型为0
,因此我们对此进行了测试.
⊂Q
makes sure to handle Q
as a whole.
0⍴
makes an empty list of that "type".
⊃
forces a prototypical element out, which is just like Q
but with all characters transformed to spaces and all numbers transformed to zeros. Now if Q
was a simple scalar number, the prototype is 0
, so we test for that.
但是,您的函数通过抛出实际错误来拒绝无效参数,而不是返回或打印结果(隐式地需要将其理解为错误),这样更合适,以便调用您函数的函数可以捕获错误并采取适当的措施.如下:
However, it would be more proper for your function to reject the invalid argument by throwing a real error rather than returning or printing a result (which implicitly needs to be understood as an error) so that a function which calls yours can trap the error and take appropriate action. As follows:
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
(可选)使用自定义消息引发错误(左侧参数),右侧参数是
⎕SIGNAL
throws an error optionally (the left argument) with a custom message, and the right argument is an error number from this list. Error number 11 is DOMAIN ERROR, which is the appropriate one in this case.
我了解您尝试使用:Try
,但是却收到了错误消息.由于您的标签和屏幕截图,我可以告诉您您正在使用Dyalog APL,其语法为:
I understood that you tried using :Try
but got an error on that. Due to your tag and your screenshot, I can tell that you are using Dyalog APL, which where the syntax is:
: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中使用的另一种错误捕获系统.)
Again, the error numbers used are those of the above linked list. (:Try
is a different error trapping system used in APLX.)
这篇关于APL中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!