ruby 的 rb_raise 如何停止调用它的 c 函数的执行?

发布于 2024-08-27 06:48:25 字数 326 浏览 3 评论 0原文

如果您将 ruby​​ 方法编写为使用 rb_raise 的 C 函数,则调用后的函数部分将不会被执行,程序将停止,您会认为 rb_raise code> 使用了 exit()。但是,如果您在 ruby​​ 中挽救异常,例如:

begin
  method_that_raises_an_exception
rescue
end
puts 'You wil still get here.'

ruby​​ 代码将继续执行,但您的函数将停止执行。 rb_raise 是如何做到这一点的?

If you write a ruby method as a function in C that uses rb_raise, the part of the function after the call will not get excecuted and the program will stop and you will think that rb_raise used exit(). But if you rescue the exception in ruby, like:

begin
  method_that_raises_an_exception
rescue
end
puts 'You wil still get here.'

The ruby code will go on, but your function will stop excecuting. How does rb_raise make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-09-03 06:48:26

据推测,它使用了setjmp(在调用该方法之前)和longjmp(在rb_raise中)。

Presumably it uses setjmp (before the method is called) and longjmp (in rb_raise).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文