BigBinary Blog

We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.

Ruby 2.5 allows rescue/else/ensure inside do/end blocks

This blog is part of our Ruby 2.5 series.

Ruby 2.4

irb> array_from_user = [4, 2, 0, 1]
  => [4, 2, 0, 1]

irb> array_from_user.each do |number|
irb>   p 10 / number
irb> rescue ZeroDivisionError => exception
irb>   p exception
irb>   next
irb> end
SyntaxError: (irb):4: syntax error, unexpected keyword_rescue,
expecting keyword_end
rescue ZeroDivisionError => exception
      ^

Ruby 2.4 throws an error when we try to use rescue/else/ensure inside do/end blocks.

Ruby 2.5.0-preview1

irb> array_from_user = [4, 2, 0, 1]
  => [4, 2, 0, 1]
irb> array_from_user.each do |number|
irb>   p 10 / number
irb> rescue ZeroDivisionError => exception
irb>   p exception
irb>   next
irb> end
2
5
#<ZeroDivisionError: divided by 0>
10
 => [4, 2, 0, 1]

Ruby 2.5 supports rescue/else/ensure inside do/end blocks.

Here is relevant commit and discussion.

Amit Choudhary in Ruby 2.5
24, 2017
Share

You might also like

Ruby 2.5 added lazy proc allocation for block parameters

Ruby 2.5 adds Thread.report_on_exception by default

Ruby 2.5 supports measuring branch and method coverages

Ruby 2.5 adds Exception#full_message method

Ruby 2.5 prints backtrace & error message in reverse

Subscribe to our newsletter