Ruby 异常的捕获

tsinghan · 2013年03月12日 · 最后由 ShiningRay 回复于 2013年03月12日 · 5285 次阅读

Don't rescue Exception, rescue StandardError

Before

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

Refactor

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

默认只能捕获 StandardError,图示说明了 ruby 的异常类型和关系。

See : http://rails-bestpractices.com/posts/702-don-t-rescue-exception-rescue-standarderror

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

不错,鼓掌,撒花

需要 登录 后方可回复, 如果你还没有账号请 注册新账号