您的位置:首页 > 编程语言 > Ruby

Ruby最佳实践--不要rescue Exception,rescue StandardError

2016-04-07 14:31 525 查看

重构前

显式地捕获Exception将会捕获甚至会捕获非正常可修复的错误比如SyntaxError, LoadError, and Interrupt。

begin
foo
rescue Exception => e
logger.warn "Unable to foo, will ignore: #{e}"
end


重构

如果你省略Exception类型标志,那么Ruby会只捕获StandardError,而这可能才是你想要的:

begin
foo
rescue => e
logger.warn "Unable to foo, will ignore: #{e}"
end


也可以参考:

http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby

原文地址

http://rails-bestpractices.com/posts/2012/11/01/don-t-rescue-exception-rescue-standarderror/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: