Exceptions

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??

Thanks,

Simon

Yes, just subclass Exception and treat it like a normal class (since
it is a normal class). I use this as some evil magic to implement a
few things.

···

On Tue, 4 Nov 2003 08:49:48 +0900 Simon Kitching simon@ecnetwork.co.nz 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??


Ryan Pavlik rpav@mephle.com

“Yeah, I mean what’re the odds of those two brain cells bumping into
each other admist all that emptiness?” - 8BT

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
def doSomething; end
def setWidget(widget); @widget = widget; end
end

e = MyException.new(“foo”, “bar”, “baz”)
e.doSomething
e.setWidget(“widget”)
raise e

Hi –

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

···

On Tue, 4 Nov 2003, Joel VanderWerf wrote:


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

Hah! the raise operator (presumably a method in Kernel?) can take an
Object as its first parameter. Cool.

That wasn’t mentioned in the Pragmatic Programmer’s book (at least not
that I saw). Maybe I should have guessed that is how it worked…

Thanks

Simon

···

On Tue, 2003-11-04 at 12:58, Joel VanderWerf wrote:

e = MyException.new(“foo”, “bar”, “baz”)
e.doSomething
e.setWidget(“widget”)
raise e

That wasn’t mentioned in the Pragmatic Programmer’s book …
yes it is,
nikolai

···


::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux[“\021%six\012\0”],(linux)[“have”]+“fun”-97);}