Hello --

On Fri, 29 Jun 2001, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "[ruby-talk:17037] keeping an Exception object alive"
>     on 01/06/29, David Alan Black <dblack / candle.superlink.net> writes:
>
> |What I'm left wondering is why there's no Exception#message= method,
> |which could be used directly and/or perhaps in the background of
> |Exception#exception.  (So that 'raise ne, "message"' would raise ne
> |itself, first changing its message, rather than creating a new
> |object.)
>
> How about making it RCR?  Since Mike Wilson has not appeared for a
> while, would somebody update the entry?  Guy, would you?

OK, I declare it an RCR :-)  And I'd be glad to put it on the wiki if
no one else does.

Details:

-- add a new method Exception#message=(aString)
-- change behavior of Exception#exception so that if the receiver is
   an Exception (or descendant) object, it returns the receiver whether
   or not a second argument (message) is specified.  Any such second
   argument becomes the receiver's message.

(This should take care of the #raise behavior described above.)

In case anyone wants to try it out, you can use this:

   class Exception
     alias :oldexception :exception
     def exception(*m)
       if kind_of? Exception
	 self.message = m[0] if m[0]
	 return self
       else
	 return oldexception(m)
       end
     end
   end

together with this patch against 1.6.4 error.c (which may be a bit
hackish in terms of namespaces and so on but should be serviceable):


*** error.c	Thu Jun 28 19:59:34 2001
--- error.c	Fri Jun 29 10:53:37 2001
***************
*** 305,306 ****
--- 305,314 ----
  static VALUE
+ exc_set_mesg(exc, mesg)
+      VALUE exc, mesg;
+ {
+   STR2CSTR(mesg); /* ensure mesg can be converted to String */
+   return rb_iv_set(exc, "mesg", mesg);
+ }
+
+ static VALUE
  exc_initialize(argc, argv, exc)
***************
*** 554,555 ****
--- 562,564 ----
      rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
+     rb_define_method(rb_eException, "message=", exc_set_mesg, 1);


(By the way, the reason I fastened on to this in the first place was
that I had added a hook to NameError and wanted to propagate the exact
object upwards, so that the stuff hanging on the hook would not
disappear.)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav