What happens when things go wrong? Guile’s exception facility exists to help answer this question, allowing programs to describe the problem and to handle the situation in a flexible way.
When a program runs into a problem, such as division by zero, it will raise an exception. Sometimes exceptions get raised by Guile on a program’s behalf. Sometimes a program will want to raise exceptions of its own. Raising an exception stops the current computation and instead invokes the current exception handler, passing it an exception object describing the unexpected situation.
Usually an exception handler will unwind the computation back to some kind of safe point. For example, typical logic for a key press driven application might look something like this:
main-loop: read the next key press and call dispatch-key dispatch-key: lookup the key in a keymap and call an appropriate procedure, say find-file find-file: interactively read the required file name, then call find-specified-file find-specified-file: check whether file exists; if not, raise an exception …
In this case,
main-loop can install an exception handler that
would cause any exception raised inside
dispatch-key to print a
warning and jump back to the main loop.
The following subsections go into more detail about exception objects, raising exceptions, and handling exceptions. It also presents a historical interface that was used in Guile’s first 25 years and which won’t be going away any time soon.