handler-case versus handler-bind

From: Alan Crowe (alan_at_cawtech.freeserve.co.uk)
Date: 01/22/04


Date: 22 Jan 2004 11:47:43 +0000

I'm trying to learn the CL condition system.
I particularly wanted to follow the thread on
Newtons method. I found the condition stuff
baffling. I couldn't see were the error was
thrown from.

I'm been working through stuff but have come
unstuck at

http://www.lispworks.com/reference/HyperSpec/Body/f_abortc.htm

    ;;; Example of the MUFFLE-WARNING restart

     (defun count-down (x)
       (do ((counter x (1- counter)))
           ((= counter 0) 'done)
         (when (= counter 1)
           (warn "Almost done"))
         (format t "~&~D~%" counter)))
    => COUNT-DOWN
     (count-down 3)
>> 3
>> 2
>> Warning: Almost done
>> 1
    => DONE
     (defun ignore-warnings-while-counting (x)
       (handler-bind ((warning #'ignore-warning))
         (count-down x)))
    => IGNORE-WARNINGS-WHILE-COUNTING
     (defun ignore-warning (condition)
       (declare (ignore condition))
       (muffle-warning))
    => IGNORE-WARNING
     (ignore-warnings-while-counting 3)
>> 3
>> 2
>> 1
    => DONE

I cut and paste this into CMUCL and it works. So I
try my own variation to check I have understood.

(defun skip-warnings-while-counting(x)
  (handler-case (count-down x)
    (warning ()
      (muffle-warning))))

(skip-warnings-while-counting 5)

5
4
3
2
Restart NIL is not active.
Error flushed ...

So there is some kind of subtle difference between
handler-case and handler-bind, but it has got me stumped.
Help!

Alan Crowe
Edinburgh
Scotland