如何在引发异常时中止Ruby脚本?

use*_*029 2 ruby exception-handling exception rescue

在Ruby中,是否有可能引发一个异常,它也会自动中止程序,忽略任何封闭的开始/救援块?

Lee*_*vis 7

不幸的是,这些exit答案都不起作用.exit提升SystemExit可以抓住.注意:

begin
  exit
rescue SystemExit
end

puts "Still here!"
Run Code Online (Sandbox Code Playgroud)

正如@dominikh所说,你需要使用exit!:

begin
  exit!
rescue SystemExit
end

puts "Didn't make it here :("
Run Code Online (Sandbox Code Playgroud)