Skip to main content

make-condition

make-condition Function

Syntax:

make-condition type &rest slot-initializations → condition

Arguments and Values:

type—a type specifier (for a subtype of condition).

slot-initializations—an initialization argument list.

condition—a condition.

Description:

Constructs and returns a condition of type type using slot-initializations for the initial values of the slots. The newly created condition is returned.

Examples:

(defvar \*oops-count\* 0) 
(setq a (make-condition ’simple-error
:format-control "This is your ~:R error."
:format-arguments (list (incf \*oops-count\*))))
→ #<SIMPLE-ERROR 32245104>
(format t "~&~A~%" a)
▷ This is your first error.
→ NIL
(error a)
▷ Error: This is your first error.
▷ To continue, type :CONTINUE followed by an option number:
▷ 1: Return to Lisp Toplevel.
▷ Debug>

Affected By:

The set of defined condition types.

See Also:

define-condition, Section 9.1 (Condition System Concepts)

Expanded Reference: make-condition

Creating a Simple Condition

make-condition constructs a condition object of the given type, initialized with the provided keyword arguments.

(make-condition 'simple-error
:format-control "File ~A not found"
:format-arguments '("data.txt"))
==> #<SIMPLE-ERROR {00000000}>

Printing a Condition

The format of the printed representation is controlled by the condition's :report option. When printed with ~A (aesthetic), the report string is shown.

(let ((c (make-condition 'simple-warning
:format-control "Disk ~A% full"
:format-arguments '(90))))
(format nil "~A" c))

=> "Disk 90% full"

Creating a Custom Condition

You can create instances of user-defined condition types.

(define-condition validation-error (error)
((field :initarg :field :reader validation-error-field)
(value :initarg :value :reader validation-error-value))
(:report (lambda (c stream)
(format stream "Validation failed for ~A: ~S"
(validation-error-field c)
(validation-error-value c)))))

(let ((c (make-condition 'validation-error
:field "email"
:value "not-an-email")))
(values (validation-error-field c)
(validation-error-value c)))

=> "email"

Passing a Condition to error

A pre-constructed condition can be passed directly to error, signal, or warn.

(let ((c (make-condition 'simple-error
:format-control "Unexpected value: ~S"
:format-arguments '(:bad))))
(handler-case (error c)
(simple-error (caught)
(format nil "~A" caught))))

=> "Unexpected value: :BAD"

Default Initform Values

If the condition type defines :initform defaults, make-condition uses them when no explicit value is supplied.

(define-condition server-error (error)
((code :initarg :code :reader server-error-code :initform 500)
(message :initarg :message :reader server-error-message
:initform "Internal Server Error"))
(:report (lambda (c stream)
(format stream "~D: ~A"
(server-error-code c)
(server-error-message c)))))

(format nil "~A" (make-condition 'server-error))

=> "500: Internal Server Error"
(format nil "~A" (make-condition 'server-error :code 404 :message "Not Found"))

=> "404: Not Found"