Problem with exception class and instance vars

Ask a Question related to Ruby, Design and Development.

  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. Similar Questions and Discussions

    1. RFC: instance::vars
      I've put together something that I don't know what to call. My working name is "instance::vars", in lowercase because it's similar to the "use...
    2. #26364 [Opn->Bgs]: initializing class in other class vars value problem
      ID: 26364 Updated by: sniper@php.net Reported By: brightone at o2 dot pl -Status: Open +Status: ...
    3. #26364 [Bgs->Opn]: initializing class in other class vars value problem
      ID: 26364 User updated by: brightone at o2 dot pl Reported By: brightone at o2 dot pl -Status: Bogus +Status: ...
    4. #26364 [NEW]: initializing class in other class vars value problem
      From: brightone at o2 dot pl Operating system: windows xp PHP version: 4.3.4 PHP Bug Type: Class/Object related Bug...
    5. Instance- and class-variables (was mixing in class methods)
      Hi -- On Thu, 2 Oct 2003, Mark J. Reed wrote: Also, every object should have the right to maintain state in instance variables that are...
  3. #2

    Default Re: Problem with exception class and instance vars

    >>>>> "B" == Bennett, Patrick <Patrick.Bennett@inin.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

  4. #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:decoux@moulon.inra.fr]
    Sent: Thursday, November 13, 2003 11:41 AM
    To: ruby-talk ML
    Cc: [email]ruby-talk@ruby-lang.org[/email]
    Subject: Re: Problem with exception class and instance vars

    >>>>> "B" == Bennett, Patrick <Patrick.Bennett@inin.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

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