Skip to main content

file-error

file-error Condition Type

Class Precedence List:

file-error, error, serious-condition, condition, t

Description:

The type file-error consists of error conditions that occur during an attempt to open or close a file, or during some low-level transactions with a file system. The “offending pathname” is initialized by the :pathname initialization argument to make-condition, and is accessed by the function file-error-pathname.

See Also:

file-error-pathname, open, probe-file, directory, ensure-directories-exist

Expanded Reference: file-error

Catching file errors

file-error is a condition type signaled when file system operations fail. It is a subtype of error.

(handler-case
(open "/tmp/nonexistent-directory-xyz/no-file.txt")
(file-error (c)
(format nil "File error: ~A" c)))
;; => "File error: ..." ; implementation-dependent message

The file-error type hierarchy

file-error is a subtype of error, which is a subtype of serious-condition.

(subtypep 'file-error 'error)
=> T
=> T

(subtypep 'file-error 'serious-condition)
=> T
=> T

Extracting the offending pathname

The file-error-pathname function retrieves the pathname that caused the error.

(handler-case
(open "/tmp/nonexistent-dir-xyz/test.txt")
(file-error (c)
(pathnamep (file-error-pathname c))))
=> T

Signaling file-error with make-condition

You can create file-error conditions programmatically with the :pathname initarg.

(let ((c (make-condition 'file-error :pathname #P"/bad/path.txt")))
(file-error-pathname c))
=> #P"/bad/path.txt"