The Lisp Way
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.