gpt4 book ai didi

c - Ruby c 扩展 : How can I catch all exceptions, 包括不是 StandardErrors 的东西?

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:56 26 4
gpt4 key购买 nike

在 ruby 中,

begin
# ...
rescue
# ...
end

不会捕获不是 StandardError 子类的异常。在 C 中,

rb_rescue(x, Qnil, y, Qnil);

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

会做同样的事情。我如何从 ruby​​ C 扩展中 rescue Exception => e(而不仅仅是 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; }

关于c - Ruby c 扩展 : How can I catch all exceptions, 包括不是 StandardErrors 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3216769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com