From: dblack@... Date: 2003-11-04T09:49:24+09:00 Subject: Re: exceptions Hi -- On Tue, 4 Nov 2003, Joel VanderWerf wrote: > Simon Kitching wrote: > > Hi, > > > > I'm puzzled about how to associate custom information with an Exception > > class I am throwing. > > > > In Java, I would do this > > > > MyException e = new MyException("foo", "bar", "baz"); > > e.doSomething(); > > e.setWidget(widget); > > // now e is all set up, so throw it > > throw e; > > > > Can this sort of thing be done in Ruby?? > > Yep: > > class MyException < Exception > def initialize(*args); @args = args; end I'm not sure what Exception's initialize does, but it might be good to call super here too. > def doSomething; end > def setWidget(widget); @widget = widget; end That's also a nice place for: attr_accessor :widget # or just attr_writer if that's what's wanted which then allows for: e.widget = "whatever" David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav