Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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


    Simon Kitching Guest

  2. Similar Questions and Discussions

    1. ANNOUNCE: POE v0.33 - Now with exceptions.
      -------------------------------------- Happy New Gregorian Year! In advance! -------------------------------------- POE 0.33 has been released....
    2. WebServices exceptions
      I just want to check my facts. I've heard some differing accounts, but my understanding of an article I read on the MSDN site is, if you throw an...
    3. Custom Exceptions
      I'm creating a Web service and a Windows Forms application to consume it. My question is about throwing a custom exception inside the WebService....
    4. Propagating exceptions or not
      Hi, I just wonder what people consider to be the best approach when it comes to propagating exceptions that has occurred in a web-service (or in...
    5. [PHP-DEV] exceptions question
      You should not use exceptions for program logic. Exceptions are use to handle exceptional cases and the overhead of handling an exception may be...
  3. #2

    Default Re: exceptions

    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

    Ryan Pavlik Guest

  4. #3

    Default Re: exceptions

    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 Guest

  5. #4

    Default Re: exceptions

    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 :: [url]http://www.ngdc.noaa.gov/stp/[/url]
    | NGDC :: [url]http://www.ngdc.noaa.gov/[/url]
    | NESDIS :: [url]http://www.nesdis.noaa.gov/[/url]
    | NOAA :: [url]http://www.noaa.gov/[/url]
    | US DOC :: [url]http://www.commerce.gov/[/url]
    |
    | 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 Guest

  6. #5

    Default Re: exceptions

    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 Guest

  7. #6

    Default Re: exceptions

    * 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: [url]www.pcppopper.org[/url] :: fun atm: gf,lps,ruby,lisp,war3 :::
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

    Nikolai Weibull Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139