From: "David A. Black" Date: 2008-07-21T05:23:17+09:00 Subject: Re: Does "rescue" wihtour argument handle any kind of Exception or not? Hi -- On Mon, 21 Jul 2008, Phlip wrote: >> Did you expect your program to do anything after printing "rescued !!!" ? > > Assuming you are addressing a senior enjinere, not a neophyte, yes I could > tell the difference between an interrupt and the rest of my program running. > It has major side-effects; I stuck the sample code at the top of my current > project. > > Curiously, this is what the original code was doing: > > begin > raise Interrupt > rescue Object > p $!.class # sez Interrupt I get ArgumentError with 1.8.6 and Interrupt with 1.9. I'm not sure why Interrupt needs an argument in 1.8.6. > end > > I didn't catch it either - 'raise' raises the given object, which here was > class Interrupt, and the metaclass itself of course does not inherit > StandardError. raise doesn't necessarily raise the object itself. It looks for an 'exception' method on the object: >> obj = Object.new => # >> def obj.exception; RuntimeError.new("Problem"); end => nil >> raise obj RuntimeError: Problem Instances of Exception (and subclasses) return themselves from #exception, so in that case you would be raising the object itself. The classes return new instances of themselves. >> r = RuntimeError.exception => # >> r.object_id => 287910 >> r.exception.object_id => 287910 If you give #exception on an instance a new message to deliver, and you get a new instance: >> r.exception("Problem").object_id => 268680 David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!