In Python's try
, except
blocks, why does else
need to exist if I can just use an except:
without a specifier?
Asked
Active
Viewed 106 times
-2

gunit
- 3,700
- 4
- 31
- 42
-
1Have you tried the second one? – khelwood Feb 16 '17 at 11:00
-
Go through this below link. Nicely explained. http://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python – Venkatesh_CTA Feb 16 '17 at 12:19
1 Answers
2
It seems like your understanding of how try
, except
, else
, and finally
is off.
Here's a summary of how they all work together, from looking at https://docs.python.org/2/tutorial/errors.html:
try:
#Try something that might raise an exception
except <exception specifier>:
#Code here will only run if the exception that came up was the one specified
except:
#Except clause without specifier will catch all exceptions
else:
#Executed if try clause doesn't raise exception
#You can only have this else here if you also have except blocks
finally:
#Runs no matter what

gunit
- 3,700
- 4
- 31
- 42