exceptions

Discussion in 'Ruby' started by Simon Kitching, Nov 3, 2003.

  1. 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
    Simon Kitching, Nov 3, 2003
    #1
    1. Advertising

  2. Simon Kitching

    Ryan Pavlik Guest

    On Tue, 4 Nov 2003 08:49:48 +0900
    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??


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

    "Yeah, I mean what're the odds of those two brain cells bumping into
    each other admist all that emptiness?" - 8BT
    Ryan Pavlik, Nov 3, 2003
    #2
    1. Advertising

  3. 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
    Joel VanderWerf, Nov 3, 2003
    #3
  4. Simon Kitching

    Ara.T.Howard Guest

    On Tue, 4 Nov 2003, 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??


    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'
    ===============================================================================
    Ara.T.Howard, Nov 4, 2003
    #4
  5. 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
    Simon Kitching, Nov 4, 2003
    #5
  6. * Simon Kitching <> [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);}
    Nikolai Weibull, Nov 4, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herr Lucifer
    Replies:
    0
    Views:
    538
    Herr Lucifer
    Jun 21, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    29,983
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,006
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    596
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    596
Loading...

Share This Page