4

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?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Sim
  • 4,199
  • 4
  • 39
  • 77

3 Answers3

10

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".

Vsevolod Dyomkin
  • 9,343
  • 2
  • 31
  • 36
2

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

hans23
  • 1,034
  • 7
  • 13
0

You could use (ignore-errors) for the purpose of silently omitting errors. Or you could call sb-ext:disable-debugger early to only see error message without the debugger launch.

peroksid
  • 890
  • 6
  • 17