Ruby 异常的捕获

tsinghan · March 12, 2013 · Last by ShiningRay replied at March 12, 2013 · 5289 hits

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

不错,鼓掌,撒花

You need to Sign in before reply, if you don't have an account, please Sign up first.