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?
UNWIND-PROTECTis an analogue offinallyclause in Java or Python, so it’s not a catch-all clause, that will intercept any unhandled condition. For that you need aHANDLER-CASEwith a handler clause for typeCONDITION.UNWIND-PROTECTactually works in your case. The only “unexpected” behaviour is that the debugger is invoked before the body ofUNWIND-PROTECTis executed. The reason for this is not to lose the current context and be able to restart execution. It is (probably) achieved viaHANDLER-BIND. You can learn more about that in PCL chapter “Conditions and Restarts”.