Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

else in try/except

20 views
Skip to first unread message

Ethan Furman

unread,
Nov 14, 2011, 4:53:29 PM11/14/11
to Python
The code in 'else' in a 'try/except/else[/finally]' block seems
pointless to me, as I am not seeing any difference between having the
code in the 'else' suite vs having the code in the 'try' suite.

Can anybody shed some light on this for me?

~Ethan~

MRAB

unread,
Nov 14, 2011, 5:59:35 PM11/14/11
to pytho...@python.org
The difference is that if an exception occurs in the else block it
won't be caught by the exception handlers of the try statement.

Arnaud Delobelle

unread,
Nov 14, 2011, 6:03:55 PM11/14/11
to Ethan Furman, Python
On 14 November 2011 21:53, Ethan Furman <et...@stoneleaf.us> wrote:
> The code in 'else' in a 'try/except/else[/finally]' block seems pointless to
> me, as I am not seeing any difference between having the code in the 'else'
> suite vs having the code in the 'try' suite.
>
> Can anybody shed some light on this for me?

Exceptions in the else clause will not be caught. Look at the
difference between the two try clauses below:

>>> def f(): raise TypeError
...
>>> try:
... print("try")
... f()
... except TypeError:
... print("except")
...
try
except
>>> try:
... print("try")
... except TypeError:
... print("except")
... else:
... f()
...
try
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "<stdin>", line 1, in f
TypeError
>>>

HTH

--
Arnaud

Chris Kaynor

unread,
Nov 14, 2011, 6:12:13 PM11/14/11
to pytho...@python.org
On Mon, Nov 14, 2011 at 2:59 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 14/11/2011 21:53, Ethan Furman wrote:
>>
>> The code in 'else' in a 'try/except/else[/finally]' block seems
>> pointless to me, as I am not seeing any difference between having the
>> code in the 'else' suite vs having the code in the 'try' suite.
>>
>> Can anybody shed some light on this for me?
>>
> The difference is that if an exception occurs in the else block it
> won't be caught by the exception handlers of the try statement.

Consider the examples:

try:
a
raise RuntimeError()
except RuntimeError:
pass

vs

try:
a
except RuntimeError:
pass
else:
raise RuntimeError()

The first example will not raise an exception, while the second will
result in a RuntimeError.

Effectively, the first block will either:
1) If the "a" block raises a RuntimeError, continue,
2) If the "a" block raised any other error, propergate it,
3) If the "a" block does not raise an exception, continue.
while the second block will either:
1) If the "a" block raises a RuntimeError, continue,
2) If the "a" block raised any other error, propergate it,
3) If the "a" block does not raise an exception, raise a RuntimeError.

Note the difference in the 3rd case.

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Ethan Furman

unread,
Nov 14, 2011, 7:15:16 PM11/14/11
to Python
Thanks, all!

~Ethan~

Barry W Brown

unread,
Nov 14, 2011, 11:53:06 PM11/14/11
to comp.lan...@googlegroups.com, Python
I thought that the point of the else clause is that it is reached only
if there is no exception in the try clause.

Barry W Brown

unread,
Nov 14, 2011, 11:53:06 PM11/14/11
to Python

Grant Edwards

unread,
Nov 15, 2011, 9:31:56 AM11/15/11
to
On 2011-11-15, Barry W Brown <brow...@gmail.com> wrote:

> I thought that the point of the else clause is that it is reached
> only if there is no exception in the try clause.

Not really. If that's all you wanted, then you just put the code at
the end of the try block.

--
Grant Edwards grant.b.edwards Yow! ... I see TOILET
at SEATS ...
gmail.com

Robert Kern

unread,
Nov 15, 2011, 10:06:15 AM11/15/11
to pytho...@python.org
On 11/15/11 2:31 PM, Grant Edwards wrote:
> On 2011-11-15, Barry W Brown<brow...@gmail.com> wrote:
>
>> I thought that the point of the else clause is that it is reached
>> only if there is no exception in the try clause.
>
> Not really. If that's all you wanted, then you just put the code at
> the end of the try block.

No, he's right. You should only put code in the try: block where you want
exceptions to be caught. Everything else should be outside of the block. You
really do want to minimize the amount of code inside the try: block.

try:
# minimal code that might raise exceptions that you want to catch
except ThisError:
# handle ThisError exceptions, and probably continue execution
except ThatError:
# handle ThatError exceptions, and probably continue execution
else:
# Code that only runs if ThisError or ThatError were not
# raised in the try: block. This code may raise ThisError or ThatError
# exceptions that should not be caught by the above except: blocks.

# Other code that runs regardless if a caught-and-continued exception
# was raised or not

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

0 new messages