Problem with exception class and instance vars

B

Bennett, Patrick

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
 
T

ts

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top