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.
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)
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.)
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
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
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
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.
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.