Finding Text of Error

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

Finding Text of Error

Post by Suroy » Mon Mar 22, 2010 9:33 pm

How would i find the text that would be displayed for any error when I catch it?
So,
(handler-case (error "HI") (error (er)
;;somehow return error string ("HI") that would've been displayed if hadn't been caught
))

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

Re: Finding Text of Error

Post by ramarren » Mon Mar 22, 2010 11:47 pm

By default, that is, when the first argument to ERROR is a string, which can be a format control string by the way, taking more arguments as arguments to format, a condition of type SIMPLE-ERROR which is a subtype of SIMPLE-CONDITION. You can retrieve the format control string and its arguments with SIMPLE-CONDITION-FORMAT-CONTROL and SIMPLE-CONDITION-FORMAT-ARGUMENTS, and then pass them to FORMAT with APPLY to construct the final string.

But you probably should create a custom condition class if you want to introspect it.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Finding Text of Error

Post by gugamilare » Tue Mar 23, 2010 11:45 am

I think what he is asking is

Code: Select all

(format t "~A" er)

Code: Select all

cl-user> (handler-case (error "HI") (error (er)
(format t "~A~%" er)))
HI
nil
If you want a string (and not to print the text), you can use (format nil "~A" er) instead.

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

Re: Finding Text of Error

Post by ramarren » Tue Mar 23, 2010 11:58 am

Ah, I forgot that while section 22.1.3 doesn't list conditions, their printing is specified explicitly. PRINC or PRINC-TO-STRING would be simpler than FORMAT for that case.

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

Re: Finding Text of Error

Post by Suroy » Tue Mar 23, 2010 2:27 pm

Exactly what i needed. Thx :D

Post Reply