Lisp allows you to use restarts - magical stuff that allows your app to recover from errors, instead of vomiting everything at you.
Lisp gives you options. Hopefully. One day, I might even be able to use them somewhat.
Here's my grossly simplified, probably wrong internal model of conditions:
- Something goes wrong
- A continuation is created, or maybe several are. Conditions are mystical "pause" buttons in the app, or something.
- The debugger appears, offering you the options
- When you pick one, lisp magicaly re-injects you where the condition was created.
- Things hopefully work a bit better.
Pretty sure this will be totally embarrasing in future.
Any way. From what I can gather, the CL condition system is implemented in terms of objects, and a 'condition' is a parent of an 'error'. 'Conditions' allow for signalling of diferent states within your app, but that's quite a fancy-pants subject, for when I know what the heck I'm doing. Typically, you'd define your error condition like this:
(define-condition slave-not-happy (error) ... other code ...)
So you're basically creating your little error as a sub-class of error, which is a subclass of condition.
In your code, you can then enable all sorts of mystical getting-out-of-jail stuff like this:
... other code ... (restart-case (error 'slave-not-happy :useable-key-name useable-key-value
(this-option ... some code)
(that-option ... some other code))
...So when you have a problem, and lisp kicks into the debugger, you'll have something like
..blah blah blah - THERE'S A PROBLEM:
0 - this-option
1 - that-option
2 - abort
3 - maybe some more options
Your stuff appears RIGHT THERE with the system's restarts, and you feel all powerful and stuff. So, basically, you're giving yourself more options for getting out of trouble.
Deciding what to do with the signalled conditions is up to you, but you can start getting really fancy-cool stuff done:
...code
(handler-bind ((slave-not-happy #'(lambda (c) (invoke-restart 'this-option)))
... other code
Of course, you can conditionally decide which restart to invoke, based on some other internal logic. Really spiffy.
here's a complete, working example you can put in a file and play around with:
Also available at
http://ryan-white-lisp.googlecode.com/files/conditions-atomic.lispUpdate:
Nikodemus Siivola (major contributor to SBCL) posted a style guide on conditions and errors:
"Learn the difference between HANDLER-BIND and HANDLER-CASE. Short version: HANDLER-CASE always unwinds, with HANDLER-BIND you can eg. log the condition without handling it, or decide whether to unwind or not after inspecting the condition in more detail."
Fantastic resources:
http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html (Kent Pitman)
http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html (Peter Siebel)
enjoy.
((((((((
(define-condition slave-not-happy (error)
((given-value :initarg :given-value :reader given-value-read))
(:report (lambda (condition stream)
(format stream "~A is not a number, dammit!" (given-value-read condition)))))
(defun slave (item)
(if (numberp item) item (restart-case (error 'slave-not-happy :given-value item) (use-nil () :report "Use nil value." nil)
(replace-item (new-value) :report "Replace the value" new-value)
(replace-item-interactively (new-value)
:report "Provide a value yourself."
:interactive (lambda ()
(format t "~&Value to use: ")
(list (eval (read))))
(slave new-value)))))
(defun master (mylist &key (on-error "ignore") (change-value 0))
(handler-bind ((slave-not-happy (if (equal on-error "change")
#'(lambda (c) (invoke-restart 'replace-item change-value)) #'(lambda (c) (invoke-restart 'use-nil)) )))
(let ((output '()) (tst ""))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output))))
(defun emperor (list)
(master list))
(defun careless-master (mylist &key)
(let ((output '()) (tst nil))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output)))
(defun micromanaging-emperor (list)
(handler-bind
((slave-not-happy #'(lambda (c) (invoke-restart 'replace-item 1000000))))
(careless-master list)))