type-error
type-error Condition Type
Class Precedence List:
type-error, error, serious-condition, condition, t
Description:
The type type-error represents a situation in which an object is not of the expected type. The “offending datum” and “expected type” are initialized by the initialization arguments named :datum and :expected-type to make-condition, and are accessed by the functions type-error-datum and type-error-expected-type.
See Also:
type-error-datum, type-error-expected-type
Expanded Reference: type-error
Handling a type-error
A type-error is signaled when an object is not of the expected type. You can catch it with handler-case.
(let ((x nil))
(handler-case
(check-type x integer)
(type-error (c)
(format nil "Got type-error: datum=~S, expected=~S"
(type-error-datum c)
(type-error-expected-type c)))))
=> "Got type-error: datum=NIL, expected=INTEGER"
Signaling a type-error Manually
(defun positive-integer-p (x)
(unless (and (integerp x) (plusp x))
(error 'type-error :datum x :expected-type '(integer 1))))
=> POSITIVE-INTEGER-P
(handler-case (positive-integer-p -5)
(type-error (c)
(type-error-datum c)))
=> -5
type-error in the Wild
Many standard functions signal type-error when given inappropriate arguments.
(handler-case (+ "a" 1)
(type-error (c)
(format nil "Expected ~A, got ~S"
(type-error-expected-type c)
(type-error-datum c))))
=> "Expected NUMBER, got \"a\""