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

标签 c ruby exception-handling ruby-c-extension rescue

在 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/

相关文章:

exception - 有什么简单的方法可以查看 Kotlin 函数抛出的异常?

c - 父子进程间通信 - 保持管道打开可以吗?

c - 使用 GSL 计算矩阵的克罗内克积的有效方法

c - 使用 txt 文件输入过滤 C 中的数据列

ruby - 如何在 ruby​​ 中拆分字符串并在拆分中维护空格

c++ - 为什么 catch 异常声明允许使用尾括号?

c# - 我怎样才能 catch 404?

c - 获取__data_start符号的地址

ruby - 如果在 1 行中返回的值为 0,我如何优雅地返回 1.0?

ruby - 形式参数不能是全局变量