Forum: Ruby reasons to use else inside rescue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-07 03:35
(Received via mailing list)
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?
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-07 04:06
(Received via mailing list)
DÅ?a Utorok 07 Február 2006 03:33 Mark Volkmann napísal:
>   handle_exception
>
> 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.

David Vallner
Confused like hell
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2006-02-07 04:30
(Received via mailing list)
On 2006.02.07 12:05, David Vallner wrote:
> > rescue
> > end
>
>
>     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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-07 10:15
(Received via mailing list)
David Vallner wrote:
>> rescue
>> end
> protect us...
Why?

> 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?

Although not being a syntax guru, the idiom you presented is definitely
*not* equivalent.  do_more_stuff will also be called if an exception was
caught and not reraised while code in the "else" branch is only invoked
if
there was no exception raised.

> 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

"finally" is Java - you probably meant "ensure".

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

As Mark said, a *rough* equivalent is to put code in the else part
directly before the first rescue.  But they do not have the same
semantics!  The difference is that all exceptions raised between "begin"
and "rescue" are potentially subject to exception handling on one of the
"rescue" branches.  This is not true for code in the "else" branch.

Another reason to put code into the else branch is documentation.  It's
visibly clear that this code does not belong to the main functionality
of
the begin end block but that it's intended to act on successfull
execution
of the block.

Kind regards

    robert
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-07 13:13
(Received via mailing list)
On 2/7/06, Robert Klemme <bob.news@gmx.net> wrote:
> >>   do_something
> >>   handle_exception
> > There's an else part in a begin / end block?! Oh dear. Heavens
> >   end
> > (apologies for using different method names):
>
> As Mark said, a *rough* equivalent is to put code in the else part
> directly before the first rescue.  But they do not have the same
> semantics!  The difference is that all exceptions raised between "begin"
> and "rescue" are potentially subject to exception handling on one of the
> "rescue" branches.  This is not true for code in the "else" branch.
>
> Another reason to put code into the else branch is documentation.  It's
> visibly clear that this code does not belong to the main functionality of
> the begin end block but that it's intended to act on successfull execution
> of the block.

Thanks!  All this makes sense now.

Even though there is some value to it, I suspect that "else" inside a
"begin" is rarely used. By "rarely", I mean less that once per hundred
"begin" blocks. I suspect this is because it's not a well known
feature and ends up making the code a bit harder to understand. I
think I'd typically tend to put the "else" code inside the "begin"
block. Does anyone disagree with this?
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-07 19:50
(Received via mailing list)
On Feb 7, 2006, at 7:10 AM, Mark Volkmann wrote:
> --
> R. Mark Volkmann
> Partner, Object Computing, Inc.


I feel like this is one of those features that I'll need to use maybe
twice. It'll just lurk there in the back of my head until I come
across a problem where using that 'else' is exactly what I'll need,
and then I'll be grateful its there. I don't think it hurts anything
by existing.
70c8da82d09d3866222976ab8978133c?d=identicon&s=25 Daniel Nugent (Guest)
on 2006-02-07 19:58
(Received via mailing list)
I think that sort of control structure would see a lot of use when
you're programming in a situation where asynchronous exceptions might
be expected.

In a case like that, you might be going a long, catch an exception,
look at it, and then conditionally, have to run off and do something
else immediately, change how you're handling what you were currently
doing, or maybe just try again.

And in each of those cases, you might have specific sorts of cleanup
code that you need to execute.

With synchronous exceptions, it's a little less useful since you
pretty much know that just giving it another shot will not help you
get the job done.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-02-07 22:34
(Received via mailing list)
Mark Volkmann <r.mark.volkmann@gmail.com> writes:

> Even though there is some value to it, I suspect that "else" inside a
> "begin" is rarely used. By "rarely", I mean less that once per hundred
> "begin" blocks. I suspect this is because it's not a well known
> feature and ends up making the code a bit harder to understand. I
> think I'd typically tend to put the "else" code inside the "begin"
> block. Does anyone disagree with this?

What if the "else code" raises an exception to (not) to be cought?
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-07 23:23
(Received via mailing list)
On 2/7/06, Christian Neukirchen <chneukirchen@gmail.com> wrote:
> Mark Volkmann <r.mark.volkmann@gmail.com> writes:
>
> > Even though there is some value to it, I suspect that "else" inside a
> > "begin" is rarely used. By "rarely", I mean less that once per hundred
> > "begin" blocks. I suspect this is because it's not a well known
> > feature and ends up making the code a bit harder to understand. I
> > think I'd typically tend to put the "else" code inside the "begin"
> > block. Does anyone disagree with this?
>
> What if the "else code" raises an exception to (not) to be cought?

In that case the exception would go to the method that called the
method containing the else ... and on up the stack.
This topic is locked and can not be replied to.