Reputation: 45
is there any way to prevent the Common Lisp console to print messages after I print out an error?
for example if i use this instruction
(error "Invalid input")
The console prints out
ERROR: Invalid Input"
1 (abort) Return to debug level 3.
2 Return to level 2.
3 Return to debug level 2.
4 Return to level 1.
5 Return to debug level 1.
6 Return to level 0.
7 Return to top loop level 0.
Is there any way to not print anything after the error message?
Upvotes: 1
Views: 303
Reputation:
Assuming you don't want to disable the debugger globally one way of doing this is to actually handle the error. CL has an extremely comprehensive system for dealing with errors and from restarting from them, but it can be used in pretty simple ways:
(defmacro aborting-from-errors (&body forms)
`(handler-case
(progn
,@forms)
(error (condition)
(format *error-output* "~&error: ~A~%" condition)
(abort condition))))
And now if I have a function like this:
(defun maybe-explode (&optional (raisep nil))
(if raisep
(error "exploded")
t))
Then
> (aborting-from-errors
(format t "~&result ~A~%" (maybe-explode))
'result)
result t
result
> (aborting-from-errors
(format t "~&result ~A~%" (maybe-explode t))
'result)
error: exploded
What is happening here is that, if an error
is raised (that is, a condition whose type is error
or some subtype of it), then aborting-from-errors
will handle that via the handler-case
it has wrapped around the code, printing it prettily to *error-output*
, and calling abort
. What abort
does is look for a restart named abort
and which is either associated with the particular condition raised or not associated with any condition, and invoke it. And the final bit of the puzzle is that the system arranges for there to be a restart named abort
available at the top level.
There are a lot of things you can do with the CL condition system, and in particular restarts are very powerful tools.
Upvotes: 1
Reputation: 139411
CL-USER 50 > (setf *debugger-hook*
(lambda (c v)
(format t "~a~%" c)
(abort c)))
#<anonymous interpreted function 4060001CBC>
CL-USER 51 > (error "Invalid input")
Invalid input
Upvotes: 4