exceptions

S

Simon Kitching

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
 
R

Ryan Pavlik

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

Joel VanderWerf

Simon said:
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
 
A

Ara.T.Howard

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

something like this?

irb(main):001:0> class MyException < Exception; def method(arg); @arg = arg; end; end
=> nil

irb(main):002:0> e = MyException.new 'foo bar baz'
=> #<MyException: foo bar baz>

irb(main):003:0> e.method 42
=> nil

irb(main):004:0> raise e
(irb):4:in `irb_binding': foo bar baz (MyException)
from /data/ruby-1.8.0//lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from /data/ruby-1.8.0//lib/ruby/1.8/irb/workspace.rb:52

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
S

Simon Kitching

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top