This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Uncatchable error

6 posts · 1007 views

(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

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:
  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
CL-USER> (test2)
"The function TEST is undefined."

Re: Uncatchable error

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

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

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

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.