On Jun 29, 2009, at 3:50 PM, Yossef Mendelssohn wrote:

> On Jun 29, 2:35 pm, Rob Biedenharn <R... / AgileConsultingLLC.com>
> wrote:
>> Since its related to 'rescue', I'll point out that you can also use
>> 'else' to indicate code that only gets executed if none of the rescue
>> clauses are (i.e., there is no exception). This is, of course, not
>> like 'ensure' with is executed regardless.
>
> Really? This is the first I've heard of this, and it seems kind of
> strange.
>
> Maybe I'm misunderstanding the point of the 'else' clause, but
> wouldn't you put code that "only gets executed if none of the rescue
> clauses are (i.e., there is no exception)" in the main body of what
> you're adding rescues to? (viz. begin block, method definition)
>
> --
> -yossef


If there is something that isn't valid in the face of an exception and  
for which you do not want to catch any exceptions, but must occur  
prior to the ensure block.

begin
   a
   b
rescue => ex
   c
else
   d
ensure
   e
end

If doing d is only valid if b completes, and assuming that (x) means  
an exception happens during x, the possible sequences are:

(a),c,e
(a),(c),e
a,(b),c,e
a,(b),(c),e
a,b,d,e
a,b,(d),e

Note that d happens before e, but (d) does not cause the rescue clause  
to be invoked.

I know that I've run into the practical need for this at least once  
(but there were several rescue clauses, not just one). If I could  
recall the specific example, I'd have put it in.

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com