Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try / else with return in try block

People also ask

Can you put a return statement in a try block?

you can use a return statement inside the try block, but you have to place another return outside the try block as well. If you pass true while calling sayHello method, it would return from try block. A return statement has to be at the method level instead of at any other specific level.

Can you return in a try?

There's no problem in returning from a try block. there's nothing wrong with it, if it's a short piece of code with a single return. Multiple returns will make it harder to refactor your code if you want to extract snippets into new methods. Notice that the finally clause will still be executed.

Can we return in except block?

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.


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. In the opposite case where no exception occurs, else runs and except doesn't.

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)

The else block isn't executed because you have left the function before it got a chance to do so.

However, the finally block is always executed (unless you yank the power cord or something like that).

Consider this (as a thought experiment; please don't do that in real code):

def whoops():
    try:
        return True
    finally:
        return False

See what it returns:

>>> whoops()
False

If you find this confusing, you're not alone. Some languages like C# actively prevent you from placing a return statement in a finally clause.


Why doesn't the else clause run?

A else clause is useful for code that must be executed if the try clause does not raise an exception.

  • When you call divide(1, 0), your try clause does raise an exception ZeroDivisionError, so the else clause does not run.

  • When you call divide(1, 1), the try clause runs successfully, and returns. So the else clause is never reached.


Why does the finally clause always run?

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not.

See above.


Reference https://docs.python.org/3.5/tutorial/errors.html


"return" ends the function and returns whatever you want it to return. So it won't go on of course. "finally" is always executed.