我知道无论是否抛出异常,确保都会运行,但是有没有办法在调用救援后只运行代码块?我问是因为如果有多个救援 block 以程序退出(或做同样的事情)结束,是否有更有效的方法在每个救援 block 的末尾调用中止或退出?
最佳答案
如何在 rescue
下重新提出异常?使用另一个开始/救援/确保/结束 block ?
def foo(error_class)
raise error_class, "raising an error..."
rescue Exception => e
begin
raise e
rescue ArgumentError
puts 'check your arguments please'
rescue LocalJumpError
puts "something's wrong with your block"
ensure
puts 'done handling the exception!'
end
end
ensure
在内部代码块中只会在发生异常后命中。
关于ruby - 在 Ruby 中救援后仅运行特定代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61809905/