11 June 2014

This tip from Roman describes the common exceptions catching bad practice, which everyone should be aware of and avoid. When writing

begin
    ...
  rescue Exception => e
    ...
  end

you will catch all errors, including SyntaxError. This may be a real problem, and the broken code will run until some customer reports it. The example two files illustrate it:

bad_file.rb:

()(())

test.rb:

begin
  require 'bad_file'
rescue Exception => e
  puts "In rescue 1."
end

begin
  require 'bad_file'
rescue => e
  puts "In rescue 2."
end

When running test.rb, it prints:

In rescue 1.
test.rb:8:in `require': ./bad_file.rb:1: syntax error, unexpected '(', expecting $end (SyntaxError)
()(())
   ^
from test.rb:8

which proves that 'rescue Exception => e' style is dangerous and catches the error you would not want to be in the code.