rb_rescue2 - throwing exception in rescue_func

Discussion in 'Ruby' started by Tobias Grimm, Aug 26, 2004.

  1. Tobias Grimm

    Tobias Grimm Guest

    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;
    }
    Tobias Grimm, Aug 26, 2004
    #1
    1. Advertising

  2. Hi,

    In message "rb_rescue2 - throwing exception in rescue_func"
    on 04/08/26, Tobias Grimm <> 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.
    Yukihiro Matsumoto, Aug 26, 2004
    #2
    1. Advertising

  3. Tobias Grimm

    Guest

    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
    , Aug 26, 2004
    #3
  4. Tobias Grimm

    Tobias Grimm Guest

    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.


    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
    Tobias Grimm, Aug 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KJ
    Replies:
    5
    Views:
    373
    John Saunders
    Jul 25, 2003
  2. VincentWong
    Replies:
    1
    Views:
    549
    VincentWong
    Dec 29, 2003
  3. =?Utf-8?B?VGVycnk=?=
    Replies:
    4
    Views:
    5,978
    =?Utf-8?B?VGVycnk=?=
    Jan 13, 2005
  4. =?Utf-8?B?R2xlbm4gVmVuemtl?=

    System.IO.Directoryinfo throwing exception

    =?Utf-8?B?R2xlbm4gVmVuemtl?=, Jul 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,683
    John Timney \(ASP.NET MVP\)
    Jul 25, 2005
  5. Edwin Pratomo

    rb_rescue2 crashes 1.9.0-0

    Edwin Pratomo, Jan 12, 2008, in forum: Ruby
    Replies:
    0
    Views:
    87
    Edwin Pratomo
    Jan 12, 2008
Loading...

Share This Page