在ruby中,

begin
  # ...
rescue
  # ...
end

不会捕获不是 StandardError.在C,

rb_rescue(x, Qnil, y, Qnil);

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

会做同样的事情。我怎样才能 rescue Exception => e 从ruby c扩展(而不仅仅是 rescue => e)?

有帮助吗?

解决方案

Ruby需要更多的文档。我不得不进入ruby源代码,这就是我发现的:

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

所以,我的问题的答案(我猜)将是:

rb_rescue2(x, Qnil, y, Qnil, rb_eException, (VALUE)0);

VALUE x(void) { /* ... */ return Qnil; }
VALUE y(void) { /* ... */ return Qnil; }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top