Skip to main content

arithmetic-error

arithmetic-error Condition Type

Class Precedence List:

arithmetic-error, error, serious-condition, condition, t

Description:

The type arithmetic-error consists of error conditions that occur during arithmetic operations. The operation and operands are initialized with the initialization arguments named :operation and :operands to make-condition, and are accessed by the functions arithmetic-error-operation and arithmetic-error-operands.

See Also:

arithmetic-error-operation, arithmetic-error-operands

arithmetic-error-operands, arithmetic-error operation Function

Syntax:

arithmetic-error-operands condition ! operands

arithmetic-error-operation condition ! operation

Arguments and Values:

condition—a condition of type arithmetic-error.

operands—a list.

operation—a function designator .

Description:

arithmetic-error-operands returns a list of the operands which were used in the o↵ending call to the operation that signaled the condition.

arithmetic-error-operation returns a list of the o↵ending operation in the o↵ending call that signaled the condition.

See Also:

arithmetic-error, Chapter 9 (Conditions)

Notes:

Expanded Reference: arithmetic-error

Type Hierarchy

The arithmetic-error condition type is a subtype of error and serves as the supertype for all arithmetic-related conditions such as division-by-zero, floating-point-overflow, floating-point-underflow, floating-point-inexact, and floating-point-invalid-operation.

(subtypep 'arithmetic-error 'error)
=> T
=> T
(subtypep 'division-by-zero 'arithmetic-error)
=> T
=> T
(subtypep 'floating-point-overflow 'arithmetic-error)
=> T
=> T

Handling Arithmetic Errors

Use handler-case to catch any arithmetic error. The accessors arithmetic-error-operation and arithmetic-error-operands provide information about what caused the error.

(handler-case (/ 1 0)
(arithmetic-error (c)
(list :operation (arithmetic-error-operation c)
:operands (arithmetic-error-operands c))))
=> (:OPERATION / :OPERANDS (1 0))

Inspecting Operation and Operands

The arithmetic-error-operation accessor returns the function that was being called, and arithmetic-error-operands returns the list of arguments that triggered the error.

(handler-case (log 0)
(arithmetic-error (c)
(format nil "~A was called on ~A"
(arithmetic-error-operation c)
(arithmetic-error-operands c))))
;; => impl-dependent

Catching Specific Subtypes

Since arithmetic-error is a parent type, you can use it as a catch-all for all arithmetic problems, or handle specific subtypes individually.

(handler-case (/ 1.0 0.0)
(division-by-zero (c)
(format nil "Division by zero: ~A on ~A"
(arithmetic-error-operation c)
(arithmetic-error-operands c)))
(arithmetic-error (c)
(format nil "Other arithmetic error: ~A" c)))
=> "Division by zero: / on (1.0 0.0)"