为什么解除错误不会在 rebol 中给出错误对象?
rebol []
secretAgent: do func[ /local person firstName lastName][
firstName: "James"
lastName: "Bond"
person: make object! [
whoAreYou: func[][
print rejoin ["My name is " lastName ", " firstName " " lastName]
]
]
]
secretAgent/whoAreYou
if (error? (error: try [secretAgent/firstName])) [
probe disarm error
]
input
返回,
My name is Bond, James Bond
** Script Error: Invalid path value: firstName
** Near: secretAgent/firstName
而我期望与
probe disarm try [secretAgent/firstName]
input
返回相同的结果:
My name is Bond, James Bond
make object! [
code: 311
type: 'script
id: 'invalid-path
arg1: 'firstName
arg2: none
arg3: none
near: [secretAgent/firstName]
where: none
]
rebol []
secretAgent: do func[ /local person firstName lastName][
firstName: "James"
lastName: "Bond"
person: make object! [
whoAreYou: func[][
print rejoin ["My name is " lastName ", " firstName " " lastName]
]
]
]
secretAgent/whoAreYou
if (error? (error: try [secretAgent/firstName])) [
probe disarm error
]
input
returns
My name is Bond, James Bond
** Script Error: Invalid path value: firstName
** Near: secretAgent/firstName
whereas I would expect same result as for
probe disarm try [secretAgent/firstName]
input
which returns:
My name is Bond, James Bond
make object! [
code: 311
type: 'script
id: 'invalid-path
arg1: 'firstName
arg2: none
arg3: none
near: [secretAgent/firstName]
where: none
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,这是 R3 降低错误触发方式的一个很好的例子。
在R2中,当出现ERROR!值被评估(由解释器处理)时,它将激活错误处理机制。因此,如果您不太小心,当您传递错误值时(例如将其传递给函数,将其作为结果返回,或者在您的情况下,在括号表达式中对其进行求值),它将触发错误再次异常处理程序。
回想起来,这个一触即发的评价规则很糟糕。因此,这就是为什么R3 不再以这种方式处理错误。但是,我们无法在 R2 中更改它。
Ah, that's a good example of why R3 tones down the way errors get triggered.
In R2, when an ERROR! values is evaluated (processed by the interpreter) it will activate the error handling mechanism. So, if you're not really careful, as you pass around the error value (like passing it to a function, returning it as a result, or in your case, evaluating it within a paren expression), it's going to trigger the error exception handler again.
In retrospect, this hair trigger was a poor evaluation rule. So, that's why R3 no longer handles errors that way. But, we cannot change it in R2.
尝试不使用额外的(括号)
REBOL 2 错误是一触即发的。您的错误是在一组括号冒泡时触发的,而不是被困住的。
请参阅此处的差异:
REBOL 3 错误处理略有不同 - 例如,不再需要 disarm。
Try without the extra (parentheses)
REBOL 2 errors are hair trigger. Your error was triggered in bubbling up one set of parentheses, rather than trapped.
See the difference here:
REBOL 3 error handling is slightly different -- disarm is not longer necessary, for example.