ignore-errors
ignore-errors Macro
Syntax:
ignore-errors {form}* → {result}*
Arguments and Values:
forms—an implicit progn.
results—In the normal situation, the values of the forms are returned; in the exceptional situation, two values are returned: nil and the condition.
Description:
ignore-errors is used to prevent conditions of type error from causing entry into the debugger.
Specifically, ignore-errors executes forms in a dynamic environment where a handler for conditions of type error has been established; if invoked, it handles such conditions by returning two values, nil and the condition that was signaled, from the ignore-errors form.
If a normal return from the forms occurs, any values returned are returned by ignore-errors.
Examples:
(defun load-init-file (program)
(let ((win nil))
(ignore-errors ;if this fails, don’t enter debugger
(load (merge-pathnames (make-pathname :name program :type :lisp)
(user-homedir-pathname)))
(setq win t))
(unless win (format t "~&Init file failed to load.~%"))
win))
(load-init-file "no-such-program")
▷ Init file failed to load.
NIL
See Also:
handler-case, Section 9.1 (Condition System Concepts)
Notes:
(ignore-errors . forms)
is equivalent to:
(handler-case (progn . forms)
(error (condition) (values nil condition)))
Because the second return value is a condition in the exceptional case, it is common (but not required) to arrange for the second return value in the normal case to be missing or nil so that the two situations can be distinguished.
Expanded Reference: ignore-errors
Basic Error Suppression
ignore-errors evaluates its body forms. If an error is signaled, instead of entering the debugger, it returns two values: nil and the condition object.
(ignore-errors (/ 1 0))
=> NIL
==> #<DIVISION-BY-ZERO {00000000}>
Normal Return
When no error is signaled, ignore-errors returns the values of the last form.
(ignore-errors (+ 1 2))
=> 3
Distinguishing Success from Error
By convention, the second return value is the condition object when an error occurs. You can use multiple-value-bind to distinguish the two cases.
(multiple-value-bind (result condition)
(ignore-errors (parse-integer "abc"))
(if condition
(format nil "Error: ~A" condition)
(format nil "Result: ~A" result)))
=> "Error: ..."
Only Catches error Conditions
ignore-errors only catches conditions of type error. Warnings and other conditions pass through.
(ignore-errors (+ 3 4))
=> 7
;; Warnings are not caught by ignore-errors
(ignore-errors
(warn "just a warning")
:done)
WARNING: just a warning
=> :DONE
Practical Example: Safe File Operation
(defun file-size-or-nil (pathname)
"Return the file size, or nil if the file cannot be accessed."
(ignore-errors
(with-open-file (s pathname :direction :input)
(file-length s))))
;; When the file does not exist:
(file-size-or-nil "/nonexistent/file.txt")
=> NIL
==> #<FILE-ERROR {00000000}>
Equivalent to handler-case
ignore-errors is a convenience macro. It is equivalent to:
(handler-case (progn (/ 10 2))
(error (c) (values nil c)))
=> 5