我正在使用 sbcl 1.0.57.0 并想通过 --eval 启动一个程序,该程序应该生成一些输出,但如果出现未捕获的错误,它将退出。

我认为最简单的方法是使用 unwind-protect:

(unwind-protect (error 'simple-error)
  (progn (FORMAT t "IAMREACHED~%") (sb-ext:exit)))

由于 (sb-ext:exit) 应该被执行,以防出现未捕获的错误。

但事实并非如此!
* (unwind-protect (error 'simple-error)

       (progn (FORMAT t "IAMREACHED~%") (sb-ext:exit)))

    debugger invoked on a SIMPLE-ERROR in thread
    #<THREAD "main thread" RUNNING {1002979193}>:
    (A SIMPLE-ERROR was caught when trying to print *DEBUG-CONDITION* when entering
    the debugger. Printing was aborted and the SIMPLE-ERROR was stored in
    SB-DEBUG::*NESTED-DEBUG-CONDITION*.)

    Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.

    restarts (invokable by number or by possibly-abbreviated name):
      0: [ABORT] Exit debugger, returning to top level.

    (#:EVAL-THUNK)
    0] 0
    IAMREACHED

我对 unwind-protect 的运作有什么误解?

最佳答案

UNWIND-PROTECT 类似于 Java 或 Python 中的 finally 子句,因此它不是一个包罗万象的子句,它会拦截任何未处理的条件。为此,您需要一个带有 HANDLER-CASE 类型的处理程序子句的 CONDITION
UNWIND-PROTECT 实际上适用于您的情况。唯一的“意外”行为是在执行 UNWIND-PROTECT 主体之前调用调试器 。这样做的原因是不会丢失当前上下文并能够重新启动执行。它(可能)是通过 HANDLER-BIND 实现的。您可以在 PCL 章节 "Conditions and Restarts" 中了解更多相关信息。

10-08 14:32