On 2006.02.07 12:05, David Vallner wrote:
> D??a Utorok 07 Febru??r 2006 03:33 Mark Volkmann nap??sal:
> > I understand that the code in the else part of a begin block is only
> > executed if no exceptions are raised by code in the begin block.
> > However, the same is true of code at the end of the begin block. Why
> > not put the code there?
> >
> > For example, I believe these are equivalent.
> >
> > begin
> >   do_something
> > rescue
> >   handle_exception
> > else
> >   do_more_stuff
> > end
> >
> > begin
> >   do_something
> >   do_more_stuff
> > rescue
> >   handle_exception
> > end
> >
> > I suppose a difference is that if "do_more_stuff" raises an exception,
> > the first example can't rescue it and the second might. Is that the
> > only difference?
> >
> > --
> > R. Mark Volkmann
> > Partner, Object Computing, Inc.
> 
> There's an else part in a begin / end block?! Oh dear. Heavens protect us...
> 
> It seems pretty equivalent to plain old:
> 
>  begin
>     do_something
>   rescue
>     handle_exception
>   end
>   do_more_stuff
> 
> Do we have a syntax guru to elaborate on this?
> 
> That said, my wild guess would be that in the code fragment (apologies for 
> using different method names):
> 
>   begin
>     foo
>   rescue
>     bar
>   else
>     baz
>   finally
>     quux
>   end
> 
> (*sic* - messiest code excerpt ever)
> 
> if #foo didn't raise an Exception, the order of executions would be #foo, 
> #baz, and then #quux. That is, unless the else is nothing more than no-op 
> syntactic sugar for just putting the statements after a begin / rescue / 
> finally block.

irb helps a lot for trying stuff out.

 >> begin
 >>   p 'foo'
 >> rescue 
 >>   p 'baz' 
 >> else 
 >>   p 'bar'
 >> ensure          # ensure, not finally :)
 >>   p 'boo' 
 >> end
 "foo"
 "bar"
 "boo"
 => nil
 >> 

> David Vallner
> Confused like hell


E