Page 1 of 1

Uncatchable error

Posted: Wed May 12, 2010 12:30 pm
by Suroy
(defun test () (error "~A"))
(defun test2 () (handler-case (test) (error (er) (princ-to-string er))))

(test2) -> throws error

However, if princ-to-string is replaced by something else, no error is thrown. I found this in two implementations (ecl and sbcl), so is there a problem with my code?

Re: Uncatchable error

Posted: Wed May 12, 2010 12:41 pm
by ramarren
If the first argument to ERROR is a FORMAT control string, like it is in your example, then there must be enough following arguments to properly format the string. In fact, SBCL explicitly indicates this when compiling the TEST function:

Code: Select all

  warning: 
    Too few arguments (0) to ERROR "~A": requires at least 1.
    See also:
      Common Lisp Hyperspec, 22.3.10.2 [:section]
In fact I don't know how did you even run the code, as on my system SBCL refuses to compile the TEST function and I just get

Code: Select all

CL-USER> (test2)
"The function TEST is undefined."

Re: Uncatchable error

Posted: Wed May 12, 2010 1:49 pm
by Suroy
Hmm, but i thought the handler-case in test2 would catch this error? It catches it if we change princ-to-string to something else.

Re: Uncatchable error

Posted: Wed May 12, 2010 2:19 pm
by ramarren
There are two separate errors here. HANDLER-CASE catches the error signalled by ERROR form, and then a new error is signalled from the handler, a formatting error as I explained before. If you don't try to print the error then the error string is not generated, and hence no formatting error.

Re: Uncatchable error

Posted: Wed May 12, 2010 2:35 pm
by Suroy
ah, i thought the error wouldn't signal itself because there was an error in the formatting which occured before the error could signal. So only one error. Guess not, thought its all easy to fix by using format.

Re: Uncatchable error

Posted: Wed Aug 11, 2010 10:25 am
by Warren Wilkinson
Errors are CLOS objects, and they just record the format string for later use. There wasn't a formating error until you tried to print it.