为什么解除错误不会在 rebol 中给出错误对象?

发布于 2024-10-04 05:03:53 字数 841 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2024-10-11 05:03:53

啊,这是 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.

纵性 2024-10-11 05:03:53

尝试不使用额外的(括号)

if error? error: try [secretAgent/firstName] [
  probe disarm error
]

REBOL 2 错误是一触即发的。您的错误是在一组括号冒泡时触发的,而不是被困住的。

请参阅此处的差异:

if error?  error: try [0 / 0]  [print ['bad mold disarm error]]
if error? (error: try [0 / 0]) [print ['bad mold disarm error]]

REBOL 3 错误处理略有不同 - 例如,不再需要 disarm

Try without the extra (parentheses)

if error? error: try [secretAgent/firstName] [
  probe disarm error
]

REBOL 2 errors are hair trigger. Your error was triggered in bubbling up one set of parentheses, rather than trapped.

See the difference here:

if error?  error: try [0 / 0]  [print ['bad mold disarm error]]
if error? (error: try [0 / 0]) [print ['bad mold disarm error]]

REBOL 3 error handling is slightly different -- disarm is not longer necessary, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文