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.

irb(main):001:0> begin
irb(main):002:1> puts "Inside begin block"
irb(main):003:1> else
irb(main):004:1> puts "Inside else block"
irb(main):005:1> end
(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.

irb(main):001:0> begin
irb(main):002:1> puts "Inside begin block"
irb(main):003:1> else
irb(main):004:1> puts "Inside else block"
irb(main):005:1> end
Traceback (most recent call last):
1: from /usr/local/bin/irb:11:in `<main>'
SyntaxError ((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.