Professional Web Applications Themes

Problem with exception class and instance vars - Ruby

I'm having problems with my Ruby module. The instance variables I'm setting in a custom exception class don't appear to be getting set. What am I doing wrong? Declaration code: EventExceptionKlass = rb_define_class("EventException", rb_eStandardError); rb_define_method(EventExceptionKlass, "event", VALUEFUNC(EventExcepGetEvent), 0); // just calls return rb_iv_get(self,"event") rb_define_method(EventExceptionKlass, "msg", VALUEFUNC(EventExcepGetMessage), 0); // just calls return rb_iv_get(self,"msg") Code where exception is raised. catch (const RecoCommonLib::EventException& ex) { VALUE throwex = rb_exc_new2( EventExceptionKlass, ex.what()); rb_iv_set(throwex, "event", rb_str_new2( [pointer to some string]) ); rb_iv_set(throwex, "msg", rb_str_new2( [pointer to another string])) ); rb_exc_raise(throwex); } When I catch the Ruby "EventException" class in a rescue class (which happens correctly) ...

  1. #1

    Default Problem with exception class and instance vars

    I'm having problems with my Ruby module.
    The instance variables I'm setting in a custom exception class don't
    appear to be getting set.
    What am I doing wrong?

    Declaration code:
    EventExceptionKlass = rb_define_class("EventException",
    rb_eStandardError);
    rb_define_method(EventExceptionKlass, "event",
    VALUEFUNC(EventExcepGetEvent), 0); // just calls return
    rb_iv_get(self,"event")
    rb_define_method(EventExceptionKlass, "msg",
    VALUEFUNC(EventExcepGetMessage), 0); // just calls return
    rb_iv_get(self,"msg")

    Code where exception is raised.

    catch (const RecoCommonLib::EventException& ex)
    {
    VALUE throwex = rb_exc_new2( EventExceptionKlass, ex.what());
    rb_iv_set(throwex, "event", rb_str_new2( [pointer to some
    string]) );
    rb_iv_set(throwex, "msg", rb_str_new2( [pointer to another
    string])) );

    rb_exc_raise(throwex);
    }

    When I catch the Ruby "EventException" class in a rescue class (which
    happens correctly) then 'event' and 'msg' are always nil (if I test
    them).
    ...
    rescue EventException => ex
    puts "Event:#{ex.event} Msg:#{ex.msg} Exception:#{ex}"
    end


    Bennett, Patrick Guest

  2. #2

    Default Re: Problem with exception class and instance vars

    >>>>> "B" == Bennett, Patrick <Patrick.Bennettinin.com> writes:

    B> What am I doing wrong?

    Difficult to say : probably ruby don't like C++ :-))

    svg% cat aa.c
    #include <ruby.h>

    static VALUE evx;

    static VALUE
    aa_tt(obj)
    VALUE obj;
    {
    VALUE thr = rb_exc_new2(evx, "<exception>");
    rb_iv_set(thr, "event", rb_str_new2("<string>"));
    rb_iv_set(thr, "msg", rb_str_new2("<another string>"));
    rb_exc_raise(thr);
    }

    static VALUE
    aa_event(obj)
    VALUE obj;
    {
    return rb_iv_get(obj, "event");
    }

    static VALUE
    aa_msg(obj)
    VALUE obj;
    {
    return rb_iv_get(obj, "msg");
    }

    void Init_aa()
    {
    evx = rb_define_class("EventException", rb_eStandardError);
    rb_define_method(evx, "event", aa_event, 0);
    rb_define_method(evx, "msg", aa_msg, 0);
    rb_define_global_function("tt", aa_tt, 0);
    }
    svg%

    svg% cat b.rb
    #!/usr/bin/ruby
    require 'aa'
    begin
    tt
    rescue EventException => ex
    puts "Event : #{ex.event} Msg : #{ex.msg} Exception : #{ex}"
    end
    svg%

    svg% b.rb
    Event : <string> Msg : <another string> Exception : <exception>
    svg%



    Guy Decoux

    ts Guest

  3. #3

    Default Re: Problem with exception class and instance vars

    Ah-ha! (or, Doh!) I had defined my 'get' methods with the wrong
    signature.
    I had set them up as if they were rb_define_method(..., -1) methods
    taking (argc, argv, self) instead of (self)
    Your test example made me realize the error of my ways. ;)
    Thanks!!

    Patrick Bennett

    -----Original Message-----
    From: ts [mailto:decouxmoulon.inra.fr]
    Sent: Thursday, November 13, 2003 11:41 AM
    To: ruby-talk ML
    Cc: [email]ruby-talkruby-lang.org[/email]
    Subject: Re: Problem with exception class and instance vars

    >>>>> "B" == Bennett, Patrick <Patrick.Bennettinin.com> writes:
    B> What am I doing wrong?

    Difficult to say : probably ruby don't like C++ :-))

    svg% cat aa.c
    #include <ruby.h>

    static VALUE evx;

    static VALUE
    aa_tt(obj)
    VALUE obj;
    {
    VALUE thr = rb_exc_new2(evx, "<exception>");
    rb_iv_set(thr, "event", rb_str_new2("<string>"));
    rb_iv_set(thr, "msg", rb_str_new2("<another string>"));
    rb_exc_raise(thr);
    }

    static VALUE
    aa_event(obj)
    VALUE obj;
    {
    return rb_iv_get(obj, "event");
    }

    static VALUE
    aa_msg(obj)
    VALUE obj;
    {
    return rb_iv_get(obj, "msg");
    }

    void Init_aa()
    {
    evx = rb_define_class("EventException", rb_eStandardError);
    rb_define_method(evx, "event", aa_event, 0);
    rb_define_method(evx, "msg", aa_msg, 0);
    rb_define_global_function("tt", aa_tt, 0);
    }
    svg%

    svg% cat b.rb
    #!/usr/bin/ruby
    require 'aa'
    begin
    tt
    rescue EventException => ex
    puts "Event : #{ex.event} Msg : #{ex.msg} Exception : #{ex}" end
    svg%

    svg% b.rb
    Event : <string> Msg : <another string> Exception : <exception> svg%



    Guy Decoux




    Bennett, Patrick Guest

Similar Threads

  1. RFC: instance::vars
    By Kevin Michael Vail in forum PERL Modules
    Replies: 2
    Last Post: May 28th, 06:42 AM
  2. Replies: 1
    Last Post: November 24th, 10:11 AM
  3. #26364 [Bgs->Opn]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 24th, 10:04 AM
  4. #26364 [NEW]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 23rd, 03:06 PM
  5. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 04:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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