A generic solution (cont'd) 1 VALUE foo(VALUE self, VALUE x) { 2 RUBY_TRY { 3 Foo f; 4 f.foo(CPP_NUM2INT(x)); 5 return Qnil; 6 } catch(std::bad_alloc & ex) { 7 RUBY_RETHROW(rb_exc_new2( 8 rb_eRuntimeError, 9 "failed to allocate memory")); 10 } RUBY_CATCH 11 }