reasons to use else inside rescue

M

Mark Volkmann

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

David Vallner

D=C5=88a Utorok 07 Febru=C3=A1r 2006 03:33 Mark Volkmann nap=C3=ADsal:
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?

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=
=20
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,=20
#baz, and then #quux. That is, unless the else is nothing more than no-op=20
syntactic sugar for just putting the statements after a begin / rescue /=20
finally block.

David Vallner
Confused like hell
 
E

Eero Saynatkari

D??a Utorok 07 Febru??r 2006 03:33 Mark Volkmann nap??sal:

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.
"foo"
"bar"
"boo"
=> nil
David Vallner
Confused like hell


E
 
R

Robert Klemme

David said:
Dňa Utorok 07 Február 2006 03:33 Mark Volkmann napísal:

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
 
D

Daniel Nugent

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

Mark Volkmann

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top