From: Niklas Frykholm Date: 2001-10-23T16:57:23+09:00 Subject: [ruby-talk:23024] Error in Programming Ruby and patch suggestion for ruby The section "Adding Information to Exceptions" on page 97 and the wording in "Raising Exceptions" on page 237 doesn't match how ruby currently works. When you do: raise object, "string" Ruby creates a clone of the object and then calls the objects initializer with the value "string". This means that in the example on page 97, @okToRetry is first set to true, but then, as the exception is raised it is set to "transient read error". I think that in this case, we should change ruby to match the documentation ;). I.e. I suggest the following patch diff -u -r1.32 error.c --- error.c 2001/10/03 07:19:10 1.32 +++ error.c 2001/10/23 07:40:57 @@ -323,10 +323,10 @@ if (argc == 0) return self; if (argc == 1 && self == argv[0]) return self; - exc = rb_obj_clone(self); - rb_obj_call_init(exc, argc, argv); + StringValue(argv[0]); + rb_iv_set(self, "mesg", argv[0]); - return exc; + return self; } static VALUE The reason is that it is currently quite hard to define your own exceptions in a good way. If you define it as: class RetryException < RuntimeError def initialize(ok) @ok = ok end end then you can never use raise object, "string". You could do class RetryException < RuntimeError def initialize(arg) if arg.is_a?(String) super(arg) else @ok = arg end end end But what if you want to use a variable @ok that is a String? You could define your own initializer class RetryException < RuntimeError def RetryException.make(ok) exc = RetryException.new() exc.ok = ok exc end end But that seems a bit ugly... By redefining exc_exception() in this way we loose the ability of initialize exceptions in the following manner: raise RetryException, true But I think that is a bad practice anyway, because it only works when the exception class takes a single argument and it is a bit confusing to read. To me, it seems simpler to always let the second argument to raise be the exception message. The change might break some code though. I've posted it as an RCR, it should appear on RubyGarden soon. // Niklas