http://docs.python.org/reference/compound_stmts.html#the-try-statement
Answer from wRAR on Stack OverflowThe optional else clause is executed if and when control flows off the end of the try clause.
Currently, control “flows off the end” except in the case of an exception or the execution of a return, continue, or break statement.
http://docs.python.org/reference/compound_stmts.html#the-try-statement
The optional else clause is executed if and when control flows off the end of the try clause.
Currently, control “flows off the end” except in the case of an exception or the execution of a return, continue, or break statement.
The reason for this behaviour is because of the return inside try.
When an exception occurs, both finally and except blocks execute before return. Otherwise only finally executes and else doesn't because the function has already returned.
This works as expected:
def divide(x, y):
print 'entering divide'
result = 0
try:
result = x/y
except:
print 'error'
else:
print 'no error'
finally:
print 'exit'
return result
print divide(1, 1)
print divide(1, 0)
What happens when you use try and except statements in a function that both have a return statement in them BUT you also have a finally statement?
python - Is it correct to use a return statement within a try and except statement? - Stack Overflow
Allow `break` and `return` inside `except*` clause - Ideas - Discussions on Python.org
Functions Try Except
Videos
Ok, that was an awful title and I'm sorry... but it's hard to phrase! My question overall is, if we use a try statement in a function (and let's imagine it works, we don't end up having to handle any exceptions) and there is a return statement in this try-block. Won't that cause us to leave the function? Since, we are returning control back to main, let's say.
But, we have a finally statement in our function to. It might do something trivial like print something. Does this get executed even though we should have hit return?
Now, I have tested this. And what it seems to do is reach the return statement, ignore it, carry out the finally statement and then go back to the return. But I would like to know if I am understanding this correctly.
Keep it simple: no try block
It took me a while to learn, that in Python it is natural, that functions throw exceptions up. I have spent too much effort on handling these problems in the place the problem occurred.
The code can become much simpler and also easier to maintain if you simply let the exception bubble up. This allows for detecting problems on the level, where it is appropriate.
One option is:
try:
return map(float, result)
except ValueError, e:
print "error", e
raise
but this introduces print from within some deep function. The same can be provided by raise which let upper level code to do what is appropriate.
With this context, my preferred solution looks:
return map(float, result)
No need to dance around, do, what is expected to be done, and throw an exception up, if there is a problem.
If you surround the code block containing a return statement with an try/except clause, you should definitely spend some thoughts of what should be returned, if an exception actually occurs:
In you example, the function will simply return None. If it's that what you want, I would suggest to explicitely add a return None like
except ValueError, e:
print "error", e
return None
in your except block to make that fact clear.
Other possibilities would be to return a "default value" (empty map in this case) or to "reraise" the exception using
except ValueError, e:
print "error", e
raise
It depends on how the function is used, under what circumstances you expect exceptions and on your general design which option you want to choose.
From the Python documentation
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. A more complicated example (having except and finally clauses in the same try statement works as of Python 2.5):
So once the try/except block is left using return, which would set the return value to given - finally blocks will always execute, and should be used to free resources etc. while using there another return - overwrites the original one.
In your particular case, func1() returns 2 and func2() returns 3, as these are values returned in the finally blocks.
It will always go to the finally block, so it will ignore the return in the try and except. If you would have a return above the try and except, it would return that value.
def func1():
try:
return 1 # ignoring the return
finally:
return 2 # returns this return
def func2():
try:
raise ValueError()
except:
# is going to this exception block, but ignores the return because it needs to go to the finally
return 1
finally:
return 3
def func3():
return 0 # finds a return here, before the try except and finally block, so it will use this return
try:
raise ValueError()
except:
return 1
finally:
return 3
func1() # returns 2
func2() # returns 3
func3() # returns 0