Add default exceptions affected by suppress (#25099)

* Add default exceptions affected by suppress

    suppress { do_something_that_might_fail }

    # instead of

    begin
      do_something_that_might_fail
    rescue
    end

    # or

    do_something_that_might_fail rescue nil

* Do not add default exceptions list constant

[Rafael Mendonça França + Alexey Zapparov]
This commit is contained in:
Alexey Zapparov 2016-05-23 22:45:01 +02:00 committed by Rafael França
parent d0a23339ac
commit 28492204ee
2 changed files with 10 additions and 0 deletions

@ -37,6 +37,7 @@ def with_warnings(flag)
#
# puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
def suppress(*exception_classes)
exception_classes = StandardError if exception_classes.empty?
yield
rescue *exception_classes
end

@ -42,6 +42,15 @@ def test_reraise
end
end
def test_suppress_with_defaults
suppress { raise RuntimeError }
suppress { raise ArgumentError }
assert_raise(LoadError) do
suppress { raise LoadError }
end
end
def test_suppression
suppress(ArgumentError) { raise ArgumentError }
suppress(LoadError) { raise LoadError }