# Reasons to use else inside rescue

**URL:** <https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856>\
**Category:** Ruby\
**Created:** [February 7, 2006, 3:35am UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856 "2006-02-07T03:35:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 3:35am UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/1 "2006-02-07T03:35:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 4:06am UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/2 "2006-02-07T04:06:13Z")

</div>

DÅ?a Utorok 07 FebruÃ¡r 2006 03:33 Mark V. 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 V.  
> 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 V.  
Confused like hell

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 4:30am UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/3 "2006-02-07T04:30:20Z")

</div>

On 2006.02.07 12:05, David V. 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 V.  
> Confused like hell

E

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 1:13pm UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/4 "2006-02-07T13:13:19Z")

</div>

On 2/7/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 10:15am UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/5 "2006-02-07T10:15:10Z")

</div>

David V. 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
```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/6 "2006-02-07T19:50:43Z")

</div>

On Feb 7, 2006, at 7:10 AM, Mark V. wrote:

> –  
> R. Mark V.  
> 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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/7 "2006-02-07T19:58:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/8 "2006-02-07T23:23:03Z")

</div>

On 2/7/06, Christian N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mark V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [February 7, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/reasons-to-use-else-inside-rescue/101856/9 "2006-02-07T22:34:04Z")

</div>

Mark V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?
