rescue => Exception e

Let’s say you want to capture exception details, like in the following example:

Problem:

def do_something!
  # ... do something ...
  success
rescue Exception => e
  failed e
end

Example:

loop do
  begin
    sleep 1
  rescue Exception => e
    puts "I'm STRONGER. Give up!"
  end
end
# Run and try CTRL+C

Using Exception => e we are allowing ALL the Exception types, as you can see above:

Exception
 NoMemoryError
 ScriptError
   LoadError
   NotImplementedError
   SyntaxError
 SignalException
   Interrupt
 StandardError
   ArgumentError
   IOError
     EOFError
   IndexError
   LocalJumpError
   NameError
     NoMethodError
   RangeError
     FloatDomainError
   RegexpError
   RuntimeError
   SecurityError
   SystemCallError
   SystemStackError
   ThreadError
   TypeError
   ZeroDivisionError
 SystemExit
 fatal

Solution:

What to do instead? Start using rescue => e as it’s the same for rescue StandardError => e and is almost certainly the broadest type of Exception that we want to rescue.

def do_something!
  # ... do something ...
  success
rescue => e
  failed e
end

Interesting Links: