common-lisphandleerror

ignore-error on Common Lisp


I'm trying to handle errors in Common Lisp easily, but I have some problems.

In particular, I have this function.

(defun function1 (m)
 (ignore-errors (and (condition-1) (condition-2))
   (format t "Error message")))

I just want that if some conditions fail into the

(and (condition-1) (condition-2)) 

it's shown the error message, otherwise just

T

How can I do? There is a better way to handle this type of errors? I looking for something very simple.

Someone can do an example showing how to use ignore-error?

Thank you.


Solution

  • You can use HANDLER-CASE

    CL-USER 101 > (handler-case (and (evenp 2)
                                     (oddp 1))
                   (error (c)
                     (princ c)
                     (values)))
    T
    
    CL-USER 102 > (handler-case (and (evenp 2)
                                     (/ 3 0)
                                     (oddp 1))
                   (error (c)
                     (princ c)
                     (values)))
    Division-by-zero caused by / of (3 0).