Rescue inside block -> syntax error

While reading this message:
http://groups.google.dk/groups?hl=da&lr=&ie=UTF-8&oe=UTF-8&selm=26163528902.20030713192844%40soyabean.com.au

What is the reason that this isn’t possible ?

And what happened to the RCR?

ruby a.rb
a.rb:3: syntax error
expand -t2 a.rb
loop do
raise 666
rescue
puts “rescue”
raise # re-raise
end

···


Simon Strandgaard

Maybe I’m not understanding what you are getting at, but:

irb(main):001:0> loop do
irb(main):002:1* begin
irb(main):003:2* raise 666
irb(main):004:2> rescue
irb(main):005:2> puts "rescue"
irb(main):006:2> raise
irb(main):007:2> end
irb(main):008:1> end
rescue
TypeError: exception class/object expected
from (irb):3:in raise' from (irb):3 from (irb):1:inloop’
from (irb):8

Seems to work fine… if you get the syntax right! As I understand it,
“begin…rescue…end” is the construct, just like “class…end” or
"def…end". None of these are blocks, though, just different language
constructs.

Or perhaps you meant something I am missing?

Chris

Maybe I’m not understanding what you are getting at, but:
[snip]
Or perhaps you meant something I am missing?

Sorry if it wasn’t clear… I wanted to express that it would
be really nice if ‘rescue’ and ‘ensure’ could be used within
do…end blocks.

wrapping things in begin…end feels superfluous :wink:

···

On Thu, 04 Mar 2004 23:33:01 +0900, Chris Pine wrote:


Simon Strandgaard

+1, and IIRC that rcr just felt in some limbo… What was wrong with it
?

···

il Thu, 04 Mar 2004 16:00:16 +0100, Simon Strandgaard neoneye@adslhome.dk ha scritto::

On Thu, 04 Mar 2004 23:33:01 +0900, Chris Pine wrote:

Maybe I’m not understanding what you are getting at, but:
[snip]
Or perhaps you meant something I am missing?

Sorry if it wasn’t clear… I wanted to express that it would
be really nice if ‘rescue’ and ‘ensure’ could be used within
do…end blocks.

wrapping things in begin…end feels superfluous :wink:

gabriele renzi wrote:

Sorry if it wasn’t clear… I wanted to express that it would
be really nice if ‘rescue’ and ‘ensure’ could be used within
do…end blocks.

wrapping things in begin…end feels superfluous :wink:

+1, and IIRC that rcr just felt in some limbo… What was wrong with it
?

For starters, it would start drifting do…end blocks and {…} blocks
apart, unless you’d allow it in those too.

{|this|
this.will
ensure
self.gag
other_who_like_curly_braces.gag
}

···

il Thu, 04 Mar 2004 16:00:16 +0100, Simon Strandgaard > neoneye@adslhome.dk ha scritto::


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_
/ö____/ (_engineering.discipline=Computer::Technology)

+10 for me! The RCR probably needs to be resubmitted in the new
format.

Cheers,
Gavin

···

On Friday, March 5, 2004, 4:14:43 AM, gabriele wrote:

il Thu, 04 Mar 2004 16:00:16 +0100, Simon Strandgaard > neoneye@adslhome.dk ha scritto::

On Thu, 04 Mar 2004 23:33:01 +0900, Chris Pine wrote:

Maybe I’m not understanding what you are getting at, but:
[snip]
Or perhaps you meant something I am missing?

Sorry if it wasn’t clear… I wanted to express that it would
be really nice if ‘rescue’ and ‘ensure’ could be used within
do…end blocks.

wrapping things in begin…end feels superfluous :wink:

+1, and IIRC that rcr just felt in some limbo… What was wrong with it
?

Kent Dahl wrote:

For starters, it would start drifting do…end blocks and {…} blocks
apart, unless you’d allow it in those too.

{|this|
this.will
ensure
self.gag
other_who_like_curly_braces.gag
}

Personally, I wouldn’t mind this. I only use {} on single lines
(as many do).

They already look very different. Why not let them behave as
differently as they look?

I do think the “generic rescue” might impose a little extra work
(a lot?) on the interpreter.

For example, when we see “begin” we know there’s a good chance
we’ll be seeing a “rescue.” Once this is changed: When we see “do”
we’re not sure whether to expect a rescue or not. Couldn’t this
impact code generation (interpreting in this case) as well as
parsing? (Disclaimer: I know little about interpreters.)

Hal

But what is the alternative? I mean, how often do you see code like
this?

meth(arg) {
begin
# …
rescue
# …
end
}

Gavin

···

On Friday, March 5, 2004, 4:24:41 AM, Kent wrote:

+1, and IIRC that rcr just felt in some limbo… What was wrong with it
?

For starters, it would start drifting do…end blocks and {…} blocks
apart, unless you’d allow it in those too.

{|this|
this.will
ensure
self.gag
other_who_like_curly_braces.gag
}

I located the lost RCR… its
RCR 105: Change do…end to support exception handling
http://rcrchive.net/rgarchive/rejected.html

matz rejected it for the reason:
rescue etc in “do … end” seem weird when a block consists a loop.

Still I think its a great RCR. +8 here :wink:

···

On Fri, 05 Mar 2004 09:22:41 +0900, Gavin Sinclair wrote:

On Friday, March 5, 2004, 4:14:43 AM, gabriele wrote:

il Thu, 04 Mar 2004 16:00:16 +0100, Simon Strandgaard

On Thu, 04 Mar 2004 23:33:01 +0900, Chris Pine wrote:

Maybe I’m not understanding what you are getting at, but:
[snip]
Or perhaps you meant something I am missing?

Sorry if it wasn’t clear… I wanted to express that it would
be really nice if ‘rescue’ and ‘ensure’ could be used within
do…end blocks.

wrapping things in begin…end feels superfluous :wink:

+1, and IIRC that rcr just felt in some limbo… What was wrong with it
?

+10 for me! The RCR probably needs to be resubmitted in the new
format.


Simon Strandgaard

“Gavin Sinclair” gsinclair@soyabean.com.au schrieb im Newsbeitrag
news:71397207684.20040305112546@soyabean.com.au…

+1, and IIRC that rcr just felt in some limbo… What was wrong with
it
?

For starters, it would start drifting do…end blocks and {…} blocks
apart, unless you’d allow it in those too.

{|this|
this.will
ensure
self.gag
other_who_like_curly_braces.gag
}

But what is the alternative? I mean, how often do you see code like
this?

meth(arg) {
begin
# …
rescue
# …
end
}

Rarely enough, true.

There’s an issue that limits the usability of do…ensure…end: Typically
you do resource allocation outside the begin…end because you don’t want
the ensure clause to be executed if something during init fails:

container.each do |obj|
processor = create_processor_in_a_way_that_might_throw()

begin
processor.do_work()
processor.do_more_work()
ensure
processor.cleanup()
end
end

or

def File.open(file, mode=“r”)
io = create_io_and_throw_if_not_found(file)

begin
yield io
ensure
io.close()
end
end

In all these cases you don’t gain anything by having do…ensure…end.

Kind regards

robert
···

On Friday, March 5, 2004, 4:24:41 AM, Kent wrote:

It’s not so weird to me, but maybe I did’nt understand.
what’s wrong with i.e.

ary.each do |data|
do stuff with data
rescue
puts 'an error occurred while doing stuff on '+data.to_s
end

seems, reasonable to me :slight_smile:

···

il Mon, 08 Mar 2004 00:04:24 +0100, Simon Strandgaard neoneye@adslhome.dk ha scritto::

I located the lost RCR… its
RCR 105: Change do…end to support exception handling
RCRS

matz rejected it for the reason:
rescue etc in “do … end” seem weird when a block consists a loop.

But wouldn’t the ruby way be to write this?

container.each do |obj|
processor.create_in_a_way_that_might_throw do |proc|
proc.do_work()
proc.do_more_work()
ensure
proc.cleanup()
end
end

Joe

···

In article c29fhn$1ptopm$1@ID-52924.news.uni-berlin.de, Robert Klemme wrote:

There’s an issue that limits the usability of do…ensure…end: Typically
you do resource allocation outside the begin…end because you don’t want
the ensure clause to be executed if something during init fails:

container.each do |obj|
processor = create_processor_in_a_way_that_might_throw()

begin
processor.do_work()
processor.do_more_work()
ensure
processor.cleanup()
end
end

“Joe Mason” joe@notcharles.ca schrieb im Newsbeitrag
news:slrnc4gj4e.9vj.joe@gate.notcharles.ca…

In article c29fhn$1ptopm$1@ID-52924.news.uni-berlin.de, Robert Klemme
wrote:

There’s an issue that limits the usability of do…ensure…end:
Typically
you do resource allocation outside the begin…end because you don’t
want
the ensure clause to be executed if something during init fails:

container.each do |obj|
processor = create_processor_in_a_way_that_might_throw()

begin
processor.do_work()
processor.do_more_work()
ensure
processor.cleanup()
end
end

But wouldn’t the ruby way be to write this?

No, because that would duplicate cleanup code.

container.each do |obj|
processor.create_in_a_way_that_might_throw do |proc|

I gues you rather mean

ProcessorClass.create_in_a_way_that_might_throw do |proc|

 proc.do_work()
 proc.do_more_work()

ensure
proc.cleanup()

The cleanup wrong here because it is done in method
create_in_a_way_that_might_throw() in this scheme after the yield (see
File.open() and below).

end
end

Joe

And how then will you implement create_in_a_way_that_might_throw()?
That’s the second example I gave - slightly changed to fit the naming:

def create_in_a_way_that_might_throw()

initialization, might throw

processor = …

begin
yield processor
ensure
processor.cleanup()
end
end

Regards

robert

But wouldn’t the ruby way be to write this?

No, because that would duplicate cleanup code.

container.each do |obj|
processor.create_in_a_way_that_might_throw do |proc|

I gues you rather mean

ProcessorClass.create_in_a_way_that_might_throw do |proc|

Right.

 proc.do_work()
 proc.do_more_work()

ensure
proc.cleanup()

The cleanup wrong here because it is done in method
create_in_a_way_that_might_throw() in this scheme after the yield (see
File.open() and below).

Oh, that’s an ensure, not an except. I was reading too fast (and I
still find Ruby’s exception keywords hard to sort out, since they’re so
different from everything else I’ve used.

Still, I think my example would be a good way to write a rescue clause
for actions only taken in case of failure, and which would benefit from
being able to put rescue in arbitrary blocks.

Joe

···

In article c29n0n$1qbumm$1@ID-52924.news.uni-berlin.de, Robert Klemme wrote:

“Joe Mason” joe@notcharles.ca schrieb im Newsbeitrag
news:slrnc4h1oi.ah6.joe@gate.notcharles.ca…

In article c29n0n$1qbumm$1@ID-52924.news.uni-berlin.de, Robert Klemme
wrote:

But wouldn’t the ruby way be to write this?

No, because that would duplicate cleanup code.

container.each do |obj|
processor.create_in_a_way_that_might_throw do |proc|

I gues you rather mean

ProcessorClass.create_in_a_way_that_might_throw do |proc|

Right.

 proc.do_work()
 proc.do_more_work()

ensure
proc.cleanup()

The cleanup wrong here because it is done in method
create_in_a_way_that_might_throw() in this scheme after the yield (see
File.open() and below).

Oh, that’s an ensure, not an except. I was reading too fast (and I
still find Ruby’s exception keywords hard to sort out, since they’re so
different from everything else I’ve used.

Still, I think my example would be a good way to write a rescue clause
for actions only taken in case of failure, and which would benefit from
being able to put rescue in arbitrary blocks.

Yeah, it sounds more reasonable for rescue clauses than ensure clauses.
Agreed.

Regards

robert