Ruby c extensions: How can I catch all exceptions, including things that are not standard?

In ruby

begin # ... rescue # ... end 

will not catch exceptions that are not subclasses of StandardError . In C,

 rb_rescue(x, Qnil, y, Qnil); VALUE x(void) { /* ... */ return Qnil; } VALUE y(void) { /* ... */ return Qnil; } 

will do the same. How can I rescue Exception => e with the ruby โ€‹โ€‹C extension (and not just rescue => e )?

+6
c ruby exception-handling rescue ruby-c-extension
source share
1 answer

Ruby needs more documentation. I had to go into the ruby โ€‹โ€‹source code, and here is what I found:

 VALUE rb_rescue(VALUE (* b_proc)(ANYARGS), VALUE data1, VALUE (* r_proc)(ANYARGS), VALUE data2) { return rb_rescue2(b_proc, data1, r_proc, data2, rb_eStandardError, (VALUE)0); } 

So the answer to my question (I think):

 rb_rescue2(x, Qnil, y, Qnil, rb_eException, (VALUE)0); VALUE x(void) { /* ... */ return Qnil; } VALUE y(void) { /* ... */ return Qnil; } 
+5
source share

All Articles