[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

reasons to use else inside rescue

Mark Volkmann

2/7/2006 2:33:00 AM

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.


6 Answers

David Vallner

2/7/2006 3:05:00 AM

0

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

David Vallner
Confused like hell



ES

2/7/2006 3:29:00 AM

0

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


Robert Klemme

2/7/2006 9:11:00 AM

0

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

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



Daniel Nugent

2/7/2006 6:58:00 PM

0

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.

On 2/7/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
>
> On Feb 7, 2006, at 7:10 AM, Mark Volkmann wrote:
> >
> > 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?
> >
> > --
> > 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.
>
>


--
-Dan Nugent


Christian Neukirchen

2/7/2006 9:33:00 PM

0

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?

> R. Mark Volkmann
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Mark Volkmann

2/7/2006 10:23:00 PM

0

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.

--
R. Mark Volkmann
Partner, Object Computing, Inc.