# Rescue on do end blocks

**URL:** <https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512>\
**Category:** Ruby\
**Created:** [March 7, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512 "2006-03-07T13:32:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/1 "2006-03-07T13:32:29Z")

</div>

I’ve been playing around with rescue expressions and besides being put  
on functions they also seem to work when defining classes (useful for  
metaprogramming perhaps?)

irb(main):001:0\> class Test  
irb(main):002:1\> raise  
irb(main):003:1\> rescue  
irb(main):004:1\> puts “Error”  
irb(main):005:1\> ensure  
irb(main):006:1\* puts “Cleaned up”  
irb(main):007:1\> end  
Error  
Cleaned up

However, why can they not be applied to do end blocks?

irb(main):016:0\> [1,2,3].each do |num|  
irb(main):017:1\* num.upcase  
irb(main):018:1\> rescue  
irb(main):019:1\> puts “Error”  
irb(main):020:1\> ensure  
irb(main):021:1\* puts “Clened up”  
irb(main):022:1\> end  
SyntaxError: compile error  
(irb):18: syntax error  
(irb):20: syntax error  
from (irb):22

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/2 "2006-03-07T14:09:16Z")

</div>

Farrel L. wrote:

> irb(main):007:1\> end  
> irb(main):021:1\* puts “Clened up”  
> irb(main):022:1\> end  
> SyntaxError: compile error  
> (irb):18: syntax error  
> (irb):20: syntax error  
> from (irb):22
> 
> Farrel

rescue must be enclosed in begin/end

[1,2,3].each do |num|  
begin #!  
num.upcase  
rescue  
puts “Error”  
ensure  
puts “Clened up”  
end  
end

lopex

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/3 "2006-03-07T14:18:21Z")

</div>

Is there some reason why it can’t be made more elegant like it is  
currently for methods? Currently you can do this for methods:

def test  
raise  
rescue  
puts “Error”  
end

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/4 "2006-03-07T14:42:46Z")

</div>

Okay that kinda makes sense… kinda.

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/5 "2006-03-07T15:16:24Z")

</div>

Farrel L. wrote:

> Okay that kinda makes sense… kinda.

🙂

Maybe somebody else will explain this better later on, but I’m sure that  
the ‘end’ of ‘do’ has nothing to do with the ‘end’ of class, def, begin,  
if, while etc…

lopex

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/6 "2006-03-07T15:25:33Z")

</div>

Ah if that’s the case then I can understand a bit more clearly why.  
Still it would be a nice thing to have.

Thanks  
Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/7 "2006-03-07T14:36:34Z")

</div>

Farrel L. wrote:

> Is there some reason why it can’t be made more elegant like it is  
> currently for methods? Currently you can do this for methods:
> 
> def test  
> raise  
> rescue  
> puts “Error”  
> end

Because do/end is another form of { } (the difference is associativity)  
and those are used to compose blocks.

Imagine:

sth.each{  
raise  
rescue  
puts “Error”  
}

which doesnt make sense for me

lopex

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 7, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/8 "2006-03-07T23:50:10Z")

</div>

On Mar 7, 2006, at 4:31 AM, Farrel L. wrote:

> I’ve been playing around with rescue expressions and besides being put  
> on functions they also seem to work when defining classes (useful for  
> metaprogramming perhaps?)

[…]

> (irb):18: syntax error  
> (irb):20: syntax error  
> from (irb):22

This will make blocks much more expensive to call because you’ll have  
to perform the exception handler setup and teardown for each  
invocation of the block which will give a slight performance  
degradation:

require ‘benchmark’

N = 10\_000\_000

Benchmark.bmbm do |bm|  
bm.report ‘without begin’ do N.times { 0 } end  
bm.report ‘with begin’ do N.times { begin 0 end } end  
bm.report ‘with rescue’ do N.times { begin 0; rescue; end } end  
end

```
                 user system total real

```

without begin 4.520000 0.020000 4.540000 ( 4.939785)  
with begin 5.000000 0.030000 5.030000 ( 6.050101)  
with rescue 6.720000 0.030000 6.750000 ( 7.716410)

While having an implicit begin won’t give the same level of slowdown  
if implemented in the compiler, there’s no pressing need to add the  
complexity to Ruby because it isn’t the common case.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 16, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/rescue-on-do-end-blocks/54512/9 "2006-03-16T21:01:38Z")

</div>

Marcin Miel¿yñski wrote:

> which doesnt make sense for me
> 
> lopex

what about this

sth.each{

```
#my code
	raise
#don't reach here

}rescue{

puts "Error"

}

```

kind like c++/java? I don’t think it looks that great either, but then  
I don’t like the {} block in my (ruby) code anyway.

just my $0.02

~S
