[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Error Trapping



;I didn't really follow the error-handling thread of Spector et. al.,
;but looking back at as much as I can in news, and reading Steele 2,
;I can't figure out what stupid mistake I am making.

(defparameter *foo* (make-instance 'sequence-dialog-item :table-sequence nil))

(index-to-cell *foo* 0)

;OK, that produced an error.  SO, try:

(handler-case (index-to-cell *foo* 0)
  (division-by-zero () 0)
)

;Hmm. I though that was supposed to trap the error.
;Let's try a bigger hammer:

(block bar
  (handler-case (index-to-cell *foo* 0)
    (division-by-zero () (return-from bar 0))
) )

;Well, maybe we need to go to a more fundamental answer:

(handler-bind ((division-by-zero #'(lambda (c) (declare (ignore c)) 0)))
  (index-to-cell *foo* 0)
)

;OK. A bigger, more fundamental hammer:

(block bar
  (handler-bind ((division-by-zero
                  #'(lambda (c) (declare (ignore c)) (return-from bar 0))
                ))
    (index-to-cell *foo* 0)
) )

;Let's see if the handler is even in effect:

(block bar
  (handler-bind ((division-by-zero
                  #'(lambda (c)
                      (format t "~S  ~S   ~S"
                              c (type-of c) (typep c 'division-by-zero)
                      )
                      (return-from bar 0)
                ))  )
    (index-to-cell *foo* 0)
) )

;So now I'm totally confused.  All I want is to ignore a division by zero
;error and return 0 instead.

;The following works, but is not pretty.
(let ((vals (multiple-value-list (ignore-errors (index-to-cell *foo* 0)))))
  ;Note that (cadr vals) isn't even 'division-by-zero!
  (if (typep (cadr vals) 'simple-error)
    0
    (car vals)
) )