From: Paul Brannan Date: 2002-04-16T00:56:31+09:00 Subject: C++ Exceptions and Ruby's catch/throw Because Ruby exceptions are implemented using longjmp, it is not safe to allow a Ruby exception to propogate over C++ code. To deal with this problem, I use rb_rescue2 around all my calls from C++ into Ruby. This is unsafe in two cases: 1) The Ruby code I am calling uses throw() 2) The Ruby code I am calling calls a continuation I don't there's much I can do about #2 except document it as having undefined behavior. However, I'd like to do something about #1. When I catch an exception, I: 1) re-throw it as a C++ exception (so it can propogate over C++ code and allow cleanup to occur), then 2) catch it as a C++ exception, and 3) re-raise it as a Ruby exception before returning to Ruby code. I can use rb_protect instead of rb_rescue2, and this will catch both exceptions and Ruby throws. But how do I then re-throw what was thrown? I.e.: extern VALUE ruby_errinfo; VALUE myfuncall(VALUE) { rb_eval_string("throw :foo"); return Qnil; } VALUE foo(VALUE self) { int state = 0; VALUE retval = rb_protect( RUBY_METHOD_FUNC(myfuncall), Qnil, &state); if(state != 0) { // ruby_errinfo is nil here } return retval; } How do I retrieve :foo from inside the foo() function? Paul