error handling—conditions are more general than exceptions in that a condition can represent any occurrence during a program's execution that may be of interest to code at different levels on the call stack. For example, in the section 'Other Uses for Conditions,' you'll see that conditions can be used to emit warnings without disrupting execution of the code that emits the warning while allowing code higher on the call stack to control whether the warning message is printed. For the time being, however, I'll focus on error handling.
The condition system is more flexible than exception systems because instead of providing a two-part division between the code that signals an error[201] and the code that handles it,[202] the condition system splits the responsibilities into three parts—
To start, I'll introduce some terminology:
So, what does it mean to handle an error? In a well-written program, each function is a black box hiding its inner workings. Programs are then built out of layers of functions: high-level functions are built on top of the lower-level functions, and so on. This hierarchy of functionality manifests itself at runtime in the form of the call stack: if high
calls medium
, which calls low
, when the flow of control is in low
, it's also still in medium
and high
, that is, they're still on the call stack.
Because each function is a black box, function boundaries are an excellent place to deal with errors. Each function—low
, for example—has a job to do. Its direct caller—medium
in this case—is counting on it to do its job. However, an error that prevents it from doing its job puts all its callers at risk: medium
called low
because it needs the work done that low
does; if that work doesn't get done, medium
is in trouble. But this means that medium
's caller, high
, is also in trouble—and so on up the call stack to the very top of the program. On the other hand, because each function is a black box, if any of the functions in the call stack can somehow do their job despite underlying errors, then none of the functions above it needs to know there was a problem—all those functions care about is that the function they called somehow did the work expected of it.
In most languages, errors are handled by returning from a failing function and giving the caller the choice of either recovering or failing itself. Some languages use the normal function return mechanism, while languages with exceptions return control by
Consider the hypothetical call chain of high
, medium
, low
. If low
fails and medium
can't recover, the ball is in high
's court. For high
to handle the error, it must either do its job without any help from medium
or somehow change things so calling medium
will work and call it again. The first option is theoretically clean but implies a lot of extra code—a whole extra implementation of whatever it was medium
was supposed to do. And the further the stack unwinds, the more work that needs to be redone. The second option—patching things up and retrying—is tricky; for high
to be able to change the state of the world so a second call into medium
won't end up causing an error in low
, it'd need an unseemly knowledge of the inner workings of both medium
and low
, contrary to the notion that each function is a black box.
Common Lisp's error handling system gives you a way out of this conundrum by letting you separate the code that actually recovers from an error from the code that decides how to recover. Thus, you can put recovery code in low-level functions without committing to actually using any particular recovery strategy, leaving that decision to code in high-level functions.
To get a sense of how this works, let's suppose you're writing an application that reads some sort of textual log file, such as a Web server's log. Somewhere in your application you'll have a function to parse the individual log entries. Let's assume you'll write a function, parse-log-entry
, that will be passed a string containing the text of a single log entry and that is supposed to return a log-entry
object representing the entry. This function will be called from a function, parse-log-file
, that reads a complete log file and returns a list of objects representing all the entries in the file.
To keep things simple, the parse-log-entry
function will not be required to parse incorrectly formatted entries. It will, however, be able to detect when its input is malformed. But what should it do when it detects bad input? In C you'd return a special value to indicate there was a problem. In Java or Python you'd throw or raise an exception. In Common Lisp, you signal a condition.
A malformed-log-entry-error
, that parse-log-entry
will signal if it's given data it can't parse.
Condition classes are defined with the DEFINE-CONDITION
macro, which works essentially the same as DEFCLASS
except that the default superclass of classes defined with DEFINE-CONDITION
is CONDITION
rather than STANDARD- OBJECT
. Slots are specified in the same way, and condition classes can singly and multiply inherit from other classes that descend from CONDITION
. But for historical reasons, condition classes aren't required to be instances of STANDARD- OBJECT
, so some of the functions you use with DEFCLASS
ed classes aren't required to work with conditions. In particular, a condition's slots can't be accessed using SLOT-VALUE
; you must specify either a :reader
option or an :accessor
option for any slot whose value you intend to use. Likewise, new condition objects are created with MAKE-CONDITION
rather than MAKE-INSTANCE
. MAKE- CONDITION
initializes the slots of the new condition based on the :initarg
s it's passed, but there's no way to further customize a condition's initialization, equivalent to