Ruby 2.6 raises exception for else without rescue

Rohan Pujari

Rohan Pujari

July 10, 2018

This blog is part of our  Ruby 2.6 series.

Ruby 2.5

If we use else without rescue inside begin..end block in Ruby 2.5, it gives a warning.

1irb(main):001:0> begin
2irb(main):002:1> puts "Inside begin block"
3irb(main):003:1> else
4irb(main):004:1> puts "Inside else block"
5irb(main):005:1> end
6(irb):5: warning: else without rescue is useless

This warning is present as code inside else block will never get executed

Ruby 2.6

In Ruby 2.6 it will raise an exception if we use else without rescue in begin..end block. This commit changed warning into exception in Ruby 2.6. Changes made in the commit are experimental.

1irb(main):001:0> begin
2irb(main):002:1> puts "Inside begin block"
3irb(main):003:1> else
4irb(main):004:1> puts "Inside else block"
5irb(main):005:1> end
6Traceback (most recent call last):
71: from /usr/local/bin/irb:11:in `<main>'
8SyntaxError ((irb):3: else without rescue is useless)

The Chinese version of this blog is available here.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.