Rb_rescue2 - throwing exception in rescue_func

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;
}

Hi,

···

In message "rb_rescue2 - throwing exception in rescue_func" on 04/08/26, Tobias Grimm <listaccount@e-tobi.net> writes:

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.

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.

···

--
Nobu Nakada

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

···

nobu.nokada@softhome.net wrote:

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.