rb_rescue2 - throwing exception in rescue_func

T

Tobias Grimm

Hi!

I still have some problems with embedding Ruby in an application.

SWIG uses rb_rescue2() to invoke methods. Now I would like to throw
a C++ exception, if Ruby raises any exception. SWIG puts this throw
statement in the rescue_func function passed to rb_rescue2().

But for some reason, I can't catch this exception from my C++ application.
(See the small sample code at the end... the exception never gets caught. It
somehow gets lost in rb_rescue())

Is it simply not possible to "transform" a Ruby exception into a C++
exception using rb_rescue2()? Will I have to rewrite SWIG to use rb_protect()?

bye,

Tobias



#include <ruby.h>
#include <iostream>

VALUE begin_func(VALUE begin_func_args)
{
return rb_funcall(begin_func_args, rb_intern("givemeanerror"), 0, NULL);
}


VALUE rescue_func(VALUE rescue_func_args, VALUE error_info)
{
cerr << "throwing exception" << endl;
throw "exception";
}

int main()
{
ruby_init();

try {
rb_rescue((VALUE(*)(ANYARGS)) begin_func, Qnil,
(VALUE(*)(ANYARGS)) rescue_func, Qnil);
}
catch (...) {
cerr << "exception caught" << endl ;
}

ruby_finalize();

return 0;
}
 
Y

Yukihiro Matsumoto

Hi,

In message "rb_rescue2 - throwing exception in rescue_func"

|Is it simply not possible to "transform" a Ruby exception into a C++
|exception using rb_rescue2()? Will I have to rewrite SWIG to use rb_protect()?

Your sample code uses rb_rescue() that only catches StandardError.
If you want to catch all exceptions, specify rb_eException to the last
argument of rb_rescue2(). I have no knowledge about SWIG generated
code, but I hope this helps.

matz.
 
N

nobu.nokada

Hi,

At Thu, 26 Aug 2004 09:28:00 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:110513]:
|Is it simply not possible to "transform" a Ruby exception into a C++
|exception using rb_rescue2()? Will I have to rewrite SWIG to use rb_protect()?

Your sample code uses rb_rescue() that only catches StandardError.
If you want to catch all exceptions, specify rb_eException to the last
argument of rb_rescue2(). I have no knowledge about SWIG generated
code, but I hope this helps.

To deal with "retry", both of rb_rescue() and rb_rescue2() use
PROT_TAG() while calling rescue_func, and throwing C++
exception leaves it. It should be fatal.
 
T

Tobias Grimm

To deal with "retry", both of rb_rescue() and rb_rescue2() use
PROT_TAG() while calling rescue_func, and throwing C++
exception leaves it. It should be fatal.

Thanks. Lyle Johnson will be so kind to replace rb_rescue2() with
rb_protect() in SWIG. This should allow custom exceptions to be
raised when the function invoked by rb_protect fails.

bye,

Tobias
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top