Fork me on GitHub

Don't rescue Exception, rescue StandardError

01 Nov 2012

Richard Bradley

Before

Explicitly rescuing Exception will rescue even not normally recoverable errors such as SyntaxError, LoadError, and Interrupt.

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

Refactor

If you omit the Exception type qualifier, then Ruby will catch only StandardError, which is probably what you want:

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

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

Tags