Exception handling in ruby extension

  • Thread starter Thomas Sondergaard
  • Start date
T

Thomas Sondergaard

Quick question,

How do you handle exceptions in a ruby extension? I see rb_rescue, but it
don't see how I get at the exception object in func2.

VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
Calls the function func1, with arg1 as the argument. If an exception occurs
during func1, it calls func2 with arg2 as the argument. The return value of
rb_rescue() is the return value from func1 if no exception occurs, from
func2 otherwise.


I want to catch a ruby exception and turn it into a .net exception, so the
code I need would look something like this

virtual System::Object *send(System::String *cmd, System::Object *array[]) {

RUBYTRY {
return marshalToDotNet(rb_apply(*_handle,
rb_intern(toStdString(cmd).c_str()),

marshalArrayFromDotNet(array)));
} RUBYCATCH (RubyException ex) {
throw new RubyDotNet::Exception(ex);
}

}

What is the best way?

Thomas
 
T

ts

T> How do you handle exceptions in a ruby extension? I see rb_rescue, but it
T> don't see how I get at the exception object in func2.

use rb_protect() then retrieve $! if you have an error.


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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top