放松保护 - 它是如何工作的

Sim*_*Sim 4 sbcl common-lisp

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

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

(unwind-protect (error 'simple-error) 
  (progn (FORMAT t "IAMREACHED~%") (sb-ext:exit)))
Run Code Online (Sandbox Code Playgroud)

至于(sb-ext:exit)应executet柜面有未被捕获的错误.

但事实并非如此!

* (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
Run Code Online (Sandbox Code Playgroud)

我对放松保护的运作有什么误解?

Vse*_*kin 9

UNWIND-PROTECTfinallyJava或Python中的类似子句,因此它不是一个catch-all子句,它将拦截任何未处理的条件.为此,您需要一个HANDLER-CASE带有类型的处理程序子句CONDITION.

UNWIND-PROTECT实际上适合你的情况.唯一的"意外"行为是执行主体之前调用调试器UNWIND-PROTECT.这样做的原因不是丢失当前上下文并且能够重新开始执行.它(可能)通过实现HANDLER-BIND.您可以在PCL章节"条件和重新启动"中了解更多相关信息.