# Exceptions : should I else or not?

**URL:** <https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966>\
**Category:** Ruby\
**Created:** [April 7, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966 "2009-04-07T21:35:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paganoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@Paganoni](https://www.ruby-forum.com/u/Paganoni)\
**Post date:** [April 7, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966/1 "2009-04-07T21:35:28Z")

</div>

Hello, when writing stuff like that

begin  
call\_this()  
do\_that()  
tell\_them()  
print “Ho yes #{my\_dear} you did it”  
rescue SomeException =\> e

# snip

end

Should I move the print statement to the else part of begin/end catch  
block ?

begin  
call\_this()  
do\_that()  
tell\_them()  
rescue SomeException =\> e

# snip

else  
print “Ho yes #{my\_dear} you did it”  
end

I presume that the result is the same, but for clarity I would prefer  
the second solution - Now, what happen if the print statement fails or  
if my\_dear() raises something ? I’ll not catch it losing the benefit of  
the begin/end. So, even if easier to read, is else really useful/a good  
idea ?

---

<div class="post-metadata">

**Author:** ![Paganoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@Paganoni](https://www.ruby-forum.com/u/Paganoni)\
**Post date:** [April 7, 2009, 10:14pm UTC](https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966/2 "2009-04-07T22:14:50Z")

</div>

Paganoni wrote:

> print “Ho yes #{my\_dear} you did it”  
> end
> 
> I presume that the result is the same, but for clarity I would prefer  
> the second solution - Now, what happen if the print statement fails or  
> if my\_dear() raises something ? I’ll not catch it losing the benefit of  
> the begin/end. So, even if easier to read, is else really useful/a good  
> idea ?

The second way gives you more control over where exceptions are caught,  
and therefore more precise knowledge of the source of the exception. As  
you point out, the print statement might raise something. If that  
happens above the rescue clause, then you don’t necessarily know what  
the source of the exception was. This is particularly important for  
common errors like NoMethodError, which can be caused by typos as well  
as by situations that are expected by the program.

---

<div class="post-metadata">

**Author:** ![Paganoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@Paganoni](https://www.ruby-forum.com/u/Paganoni)\
**Post date:** [April 8, 2009, 2:55am UTC](https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966/3 "2009-04-08T02:55:45Z")

</div>

On Tue, Apr 7, 2009 at 12:34 PM, Paganoni [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â print “Ho yes #{my\_dear} you did it”  
> end
> 
> I presume that the result is the same, but for clarity I would prefer the  
> second solution - Now, what happen if the print statement fails or if  
> my\_dear() raises something ? I’ll not catch it losing the benefit of the  
> begin/end. So, even if easier to read, is else really useful/a good idea ?

Yes, often, because you should (in general) only be rescuing  
exceptions if you can do something useful with them where you are,  
otherwise, you should be letting them bubble up. So if the errors you  
are going to deal with aren’t the one’s in the print line, it makes  
sense to have it in the else. Of course, if you different logic to  
deal with errors in the print statement, it even make make sense to  
do:

begin  
.  
.  
.  
rescue SomeException =\> e  
.  
.  
.  
else  
begin  
print “Ho yes #{my\_dear} you did it”  
rescue SomeException =\> e  
.  
.  
.  
end  
end

---

<div class="post-metadata">

**Author:** ![Paganoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@Paganoni](https://www.ruby-forum.com/u/Paganoni)\
**Post date:** [April 8, 2009, 10:57am UTC](https://www.ruby-forum.com/t/exceptions-should-i-else-or-not/164966/4 "2009-04-08T10:57:05Z")

</div>

On Tue, Apr 7, 2009 at 9:34 PM, Paganoni [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, when writing stuff like that  
> Hmm I am speaking up againts very learned folks, so I might be wrong (  
> but that is true anyway ;).

Now I think that

begin  
do\_stuff  
other\_stuff  
except  
whatever  
end

is easier to read than

begin  
do\_stuff  
except  
whatever  
else  
other\_stuff  
end

This however does not invalidate the very sound reasons Joel et altri  
have given to move some code into the else clause.  
What to do then? Maybe it all depends on the context (thank you Andy;)?  
If you are catching a very general exception e.g.  
except  
except StandardError  
except Exception  
it is probably indeed wise to chose the else for your “ordinary” code.

However if do\_stuff might raise a very specific exception I would  
prefer the “simpler”

begin  
do\_stuff  
other\_stuff  
except SpecificException =\> se  
whatever se  
end

Just my micromoney.  
Cheers  
Robert

There are some people who begin the Zoo at the beginning, called  
WAYIN, and walk as quickly as they can past every cage until they get  
to the one called WAYOUT, but the nicest people go straight to the  
animal they love the most, and stay there. ~ A.A. Milne (from  
Winnie-the-Pooh)
