Uncatchable error

Discussion of Common Lisp
Post Reply
Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Uncatchable error

Post by Suroy » Wed May 12, 2010 12:30 pm

(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?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Uncatchable error

Post by ramarren » Wed May 12, 2010 12:41 pm

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."

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: Uncatchable error

Post by Suroy » Wed May 12, 2010 1:49 pm

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Uncatchable error

Post by ramarren » Wed May 12, 2010 2:19 pm

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.

Suroy
Posts: 46
Joined: Sat Dec 19, 2009 11:20 am

Re: Uncatchable error

Post by Suroy » Wed May 12, 2010 2:35 pm

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.

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: Uncatchable error

Post by Warren Wilkinson » Wed Aug 11, 2010 10:25 am

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.
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

Post Reply