Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unwind-protect - how does it work

I am using sbcl 1.0.57.0 and want to start a program via --eval which should generate some output, but in case that there is an uncaught error it shall exit.

I figured the easiest way to accomplish that would be by using unwind-protect:

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

As (sb-ext:exit) should be executet incase there is an uncaught error.

But it doesn't!

* (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

What is my misconception about the workings of unwind-protect?

like image 940
Sim Avatar asked Dec 16 '22 18:12

Sim


2 Answers

UNWIND-PROTECT is an analogue of finally clause in Java or Python, so it's not a catch-all clause, that will intercept any unhandled condition. For that you need a HANDLER-CASE with a handler clause for type CONDITION.

UNWIND-PROTECT actually works in your case. The only "unexpected" behaviour is that the debugger is invoked before the body of UNWIND-PROTECT is executed. The reason for this is not to lose the current context and be able to restart execution. It is (probably) achieved via HANDLER-BIND. You can learn more about that in PCL chapter "Conditions and Restarts".

like image 115
Vsevolod Dyomkin Avatar answered Jan 05 '23 00:01

Vsevolod Dyomkin


Maybe you want to disable the debugger, see SBCL Toplevel Options

like image 43
hans23 Avatar answered Jan 05 '23 00:01

hans23