[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

exceptions

Simon Kitching

11/3/2003 11:50:00 PM

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


5 Answers

Ryan Pavlik

11/3/2003 11:55:00 PM

0

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

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.

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

Joel VanderWerf

11/3/2003 11:58:00 PM

0

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


Ara.T.Howard

11/4/2003 12:11:00 AM

0

Simon Kitching

11/4/2003 1:09:00 AM

0

On Tue, 2003-11-04 at 12:58, Joel VanderWerf wrote:
> e = MyException.new("foo", "bar", "baz")
> e.doSomething
> e.setWidget("widget")
> raise e
>

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


Nikolai Weibull

11/4/2003 12:31:00 PM

0

* Simon Kitching <simon@ecnetwork.co.nz> [Nov, 04 2003 10:40]:
> 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);}