About signaling conditions in Common Lisp, Nikodemus Siivola writes:
Use #'ERROR to signal conditions inheriting from SERIOUS-CONDITION. This includes all subclasses or ERROR. SERIOUS-CONDITION is a convention that means "this will land you in the debugger if not handled", which is what #'ERROR ensures.
- Inherit from ERROR if unwinding from the error leaves the system in a consistent state. Subclasses of ERROR mean "oops, we have a problem, but it's OK to unwind."
- Inherit from SERIOUS-CONDITION, not ERROR, if the system is messed up somehow, and/or requires either human intervention or special knowledge about the situation to resolve: SERIOUS-CONDITIONs that are not ERRORs mean that "something is deeply wrong and you should know what you are doing if you are going to resolve this."
There are two problems with this interpretation.
First, it gives meaning to a difference type: (and serious-condition (not error))
means something beyond what serious-condition
alone means. You could interpret this as “serious-condition
means we can't continue; error
means we can exit safely” - but non-serious-condition
signals also mean we can exit safely. So the conditions that can be handled safely are (and condition (or (not serious-condition) error))
. This complex type is at least a warning sign. If you want to distinguish recoverable from unrecoverable conditions, shouldn't one or the other have its own class?
Second, it's not what the Hyperspec says. It defines an error as
a situation in which the semantics of a program are not specified, and in which the consequences are undefined
and a serious condition as
a situation that is generally sufficiently severe that entry into the debugger should be expected if the condition is signaled but not handled.
These aren't the most lucid definitions, but the idea is that serious-condition
means the signal can't be ignored, and error
means this is because the semantics are undefined. (and serious-condition (not error))
, therefore, is a situation where the semantics are defined, but where execution nonetheless cannot continue for some other reason - running out of memory, for instance. The distinction isn't between well-behaved states and messed-up ones, but between semantic nonsense and other failures. C++ has an analogous distinction between logic_error
, which is the same as CL's error
, and runtime_error
, which covers the rest of CL's serious-condition
. (C++ has only terminating exceptions, so it doesn't have non-serious conditions.)
I'm tempted to make more such distinctions, giving something like this:
- Nonsense: the program has asked for something undefined, like
(+ '(hello) "world!")
. (CLerror
, C++logic_error
) You can define a reasonable meaning for most nonsense errors; they are errors only because their meaning hasn't been defined yet. - Limitation: the implementation knows what you mean, but can't deliver, e.g. arithmetic overflow or running out of memory. (RNRS calls this a “violation of an implementation restriction”.) You could consider “not yet implemented” to be a limitation as well.
- Environmental failure: the operation failed because the outside world did not cooperate, e.g. a file was not found or a connection was refused.
- Forbidden: operations which do have an well-defined meaning, but you're not allowed to do them, usually for security or safety reasons, e.g. you don't have permission to
unlink("/")
.
In other words, “I don't understand”, “I can't”, “It's not working”, and “I'm sorry, Dave. I'm afraid I can't do that.”.
This is a nice taxonomy, but I'm not sure it's actually useful. The problem is that one error often leads to another, and the error that's detected isn't necessarily the original problem. For instance, failure to open a nonexistent file (an environmental failure) may manifest as a nonsensical attempt to read from an invalid stream. (CL actually signals it as an error
, even though it's not a semantic error, perhaps for this reason.) Or a fixed-size buffer (a limitation) causes a buffer overflow (nonsense) which is eventually detected as a segfault (a forbidden operation). Why try to distinguish between classes of errors when they're so fluid?
Fortunately, programs don't usually care why they failed, unless it was a certain specific error they were expecting. IME most exception handlers are either quite broad (e.g. catching serious-condition
near the root of a large computation), and recover in generic ways such as aborting, or quite specific (e.g. catching file-not-found-error
on one particular open
), and attempt to deal with the problem. There aren't many handlers that try to generalize at intermediate levels, even in languages that support them.
So maybe it's not so useful to classify exceptions by their cause. Maybe we should instead classify them by the possible responses: things like whether they can be ignored (CL's serious-condition
) and whether they can be handled at all. In that case Nikodemus' interpretation of (and serious_condition (not error))
as “unrecoverable error” is right — not for CL, but for some future language.
FTR reddit submission I myself, not so sure. Maybe overthinking things? Maybe two separate error messages, one for the error message, and one for what to do.
ReplyDeleteI believe this whole part of the condition hierarchy is just a mistake. Condition objects should describe what the exceptional situation is, not what the raiser thinks can or cannot be done. The correct place to put the information that I don't want to hear from you again after you've handled this error is at the point of raise. Hence the R6RS procedures raise and raise-continuably.
ReplyDelete