Skip to main content

program-error

program-error Condition Type

Class Precedence List:

program-error, error, serious-condition, condition, t

Description:

The type program-error consists of error conditions related to incorrect program syntax. The errors that result from naming a go tag or a block tag that is not lexically apparent are of type program-error.

Expanded Reference: program-error

Type Hierarchy

program-error is a subtype of error that represents errors resulting from incorrect program syntax or semantics detected at runtime (e.g., wrong number of arguments, invalid keyword arguments).

(subtypep 'program-error 'error)
=> T
=> T
(subtypep 'program-error 'condition)
=> T
=> T

Handling program-error with handler-case

A program-error may be signaled when calling a function with the wrong number of arguments or with invalid keyword arguments.

;; Calling a function with wrong number of arguments
(handler-case
(funcall (lambda (x) x) 1 2 3)
(program-error (c)
(format nil "Caught program-error: ~A" c)))
;; => "Caught program-error: ..." (message is implementation-dependent)

Checking with typep

You can inspect a captured condition to confirm it is a program-error.

;; Capture and inspect the condition
(let ((condition
(handler-case
(funcall (lambda (x) x)) ; too few arguments
(error (c) c))))
(values (typep condition 'program-error)
(typep condition 'error)))
=> T
=> T

Signaling program-error Manually

You can signal a program-error in your own code to indicate a static programming mistake.

(defun validate-mode (mode)
(unless (member mode '(:read :write :append))
(error 'program-error
:format-control "Invalid mode: ~S. Expected :READ, :WRITE, or :APPEND."
:format-arguments (list mode))))

(handler-case (validate-mode :delete)
(program-error (c)
(format nil "Caught: ~A" c)))
;; => "Caught: ..." (message is implementation-dependent)