---
title: "Ruby 2.5 allows rescue/else/ensure inside do/end blocks"
description: "rescue/else/ensure are allowed inside do/end blocks in ruby 2.5"
canonical_url: "https://www.bigbinary.com/blog/ruby-2.5-allows-rescue-inside-do-end-blocks"
markdown_url: "https://www.bigbinary.com/blog/ruby-2.5-allows-rescue-inside-do-end-blocks.md"
---

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

rescue/else/ensure are allowed inside do/end blocks in ruby 2.5

- Author: Amit Choudhary
- Published: October 24, 2017
- Categories: Ruby 2.5, Ruby

#### Ruby 2.4

```ruby
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

```ruby
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](https://github.com/ruby/ruby/commit/0ec889d7ed) and
[discussion](https://bugs.ruby-lang.org/issues/12906).

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2.5-allows-rescue-inside-do-end-blocks)
