Which is the shortest way to silently ignore a Ruby exception
I'm looking for something like this:
raise Exception rescue nil
But the shortest way I've found is this:
begin
raise Exception
rescue Exception
end
def ignore_exception
begin
yield
rescue Exception
end
end
现在写你的代码
ignore_exception { puts "Ignoring Exception"; raise Exception; puts "This is Ignored" }
This is provided by ActiveSupport:
suppress(Exception) do
# dangerous code here
end
http://api.rubyonrails.org/classes/Kernel.html#method-i-suppress
Just wrap the left-hand side in parenthesis:
(raise RuntimeError, "foo") rescue 'yahoo'
Note that the rescue will only happen if the exception is a StandardError or a subclass thereof. See http://ruby.runpaint.org/exceptions for more info.
链接地址: http://www.djcxy.com/p/25842.html上一篇: 开始救援不捕捉错误
下一篇: 这是最简单的方式来默默地忽略Ruby异常