← Blog

Ruby 2.6 raises exception for else without rescue

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.

Rohan Pujari avatar
Written byRohan Pujari
Newsletter

Stay up to date with our blogs.

Subscribe to receive an email notification for a new blog post.