LISP Error Handling - Lisp programming

What is Error Handling in LISP?

  • In Common LISP terminology, exceptions are called conditions.
  • In traditional programming languages, the conditions are more general than exceptions.
  • The condition represents any occurrence, error, or not, which might affect various levels of function call stack.
  • Condition handling mechanism in LISP, handles the conditions are used to signal warning while the upper level code on the call stack can continue its work.

In LISP, the condition handling system has three parts:

  • Signalling a condition
  • Handling the condition
  • Restart the process

Handling a Condition

An example for handling a condition arising out of divide by zero condition is illustrated here.
The following steps are used for handling a condition:

  • Define the Condition :
    "A condition is an object whose class indicates the general nature of the condition and whose instance data carries information about the details of the particular circumstances that lead to the condition being signalled".
    The condition is defined by using define-condition macro.


New condition objects are created with MAKE-CONDITION macro, which initializes the slots of the new condition based on the :initargs argument.
The following code defines the condition in the below example:

  • Writing the Handlers:
    A code used for handling the condition signalled thereon is called condition handler.
    This is one of the higher level functions that call the erroring function.
    When a condition is signalled, the signalling mechanism searches for an appropriate handler based on the condition's class.
    Each handler consists of:

    • Type specifier, that indicates the type of condition it can handle
    • A function that takes a single argument, the condition

The signalling mechanism finds the most recently established handler that is compatible with the condition type and calls its function, when a condition is signalled.
The macro handler-case establishes a condition handler. The basic form of a handler-case:

The form of each error clause:

  • Restarting Phase
    A program is recovered by this code from errors, and condition handlers can then handle a condition by invoking an appropriate restart.
    The restart code is generally place in middle-level or low-level functions and the condition handlers are placed into the upper levels of the application.

The handler-bind macro allows you to provide a restart function, and allows you to continue at the lower level functions without unwinding the function call stack.
The flow of control will still be in the lower level function.
The basic form of handler-bind is as follows:

Where each binding is a list of the following:

  • a condition type
  • a handler function of one argument

The invoke-restart macro finds and invokes the most recently bound restart function with the specified name as argument.
You can have multiple restarts.


The above concepts are demonstrated by writing a function named division-function, which will create an error condition if the divisor argument is zero.
There are three anonymous functions that provide three ways to come out of it - by returning a value 1, by sending a divisor 2 and recalculating, or by returning 1.

Create a new source code file named main.lisp and type the following code in it.

When you execute the code, it returns the following result:

The 'Condition System' discussed above in Common LISP also provides various functions that may be called for signalling an error. Handling of an error, when signalled, is however, implementation-dependent.

Error Signalling Functions in LISP

  • The commonly used functions signalling warnings, breaks, non-fatal and fatal errors are provided in the below table:
  • The user program specifies an error message (a string). The functions process this message and may/may not display it to the user.
  • The error messages should be constructed by applying the format function, should not contain a newline character at either the beginning or end, and need not indicate error, as the LISP system will take care of these according to its preferred style.
SL No.
Functions and Descriptions
It signals a fatal error. It is impossible to continue from this kind of error; thus error will never return to its caller.
cerrorcontinue-format-string error-format-string&restargs
It signals an error and enters the debugger. However, it allows the program to be continued from the debugger after resolving the error.
it prints an error message but normally doesn't go into the debugger
It prints the message and goes directly into the debugger, without allowing any possibility of interception by programmed error-handling facilities


In this example, the factorial function calculates factorial of a number; however, if the argument is negative, it raises an error condition.

Create a new source code file named main.lisp and type the following code in it.

When you execute the code, it returns the following result:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Lisp programming Topics