Ruby

Ruby

Made by DeepSource

Useless occurrence of rescue RB-W1023

Anti-pattern
Major

Avoid bubbling up exceptions with useless rescues (which only re-raise the errors) in your code as they are inefficient and can make the control flow difficult to understand.

Bad practice

# bad
def foo
  do_something
rescue
  raise
end

# bad
def foo
  do_something
rescue => e
  raise # or 'raise e', or 'raise $!', or 'raise $ERROR_INFO'
end

# bad (last rescue does not do anything)
def foo
  do_something
rescue ArgumentError
  # noop
rescue
  raise
end

Recommended

def foo
rescue => e
ensure
end

def foo
  do_something
rescue
  do_cleanup
raise e
end

# Doesnt raise the same error
def foo
  do_something
rescue => e
  raise x
end

References

  1. Performance of Exception Handling in Ruby