4๐
If there is no exception in the try:
suite, then the else:
suite is executed. In other words, only if there is an actual exception is the except:
suite reached and the return
statement used.
In my view, the return
statement is what is redundant here; a pass
would have sufficed. Iโd use an else:
suite to a try
when there is additional code that should only be executed if no exception is raised, but could raise exceptions itself that should not be caught.
You are right that a return
in the except
clause makes using an else:
for that section of code somewhat redundant. The whole suite could be de-dented and the else:
line removed:
def foo():
try:
# Some code
except:
# Some code
return
# Some code
4๐
From the docs:
The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasnโt raised by the code being protected by the try ... except statement.
http://docs.python.org/2/tutorial/errors.html#handling-exceptions
- [Django]-How to get checkbox value in django?
- [Django]-Can I serve one django app from 2 different apache vhosts?
- [Django]-Django how to upload folder