简体   繁体   中英

finally block execute before the break,continue,return statement of try clause in python

finally block execute before the break or continue or return statement of try clause in python then why the output of this code is different

def bool_return():
    try:
        return print("foo")
    except:
        pass
    finally:
        return "ola"

print(bool_return())

output is:

foo ola

why ?

Returning something is the last thing a function can do. After returning something, Python will continue doing what it did before or end the script.

If Python would return immediately, this would be the end of the function. The finally-block is thought to be ran no matter what happens, therefore it has to run first, even if the original return gets lost.

the finally block is some thing where we write the code for closing the connections to other servers . If in case we returned from the statement we will have the connections opened so it will be executed before moving out of function. We can check if we write a return in final block and try block which one is executing

Your code

def bool_return():
    try:
        return print("foo")
    except:
        pass
    finally:
        return print("ola")

print(bool_return())

is using a finally clause. The finally clause is executed before the exiting from the owning try / except block. This means that your could is turned in to the following (pseudocode!):

function bool_return():
    do in case of errors goto Errors:
        var r = print("foo") < this is the statement in the try block
        print("ola") < this is the statement in the finally one
        return r
Errors:
        do nothing < this is the pass instruction.
        var r = print ("ola")
        return r 

print(bool_return())

Ignore for a moment the return statement which might not be correct, the culprit of your question is how the interpreter runs the statements:

 return print("foo")

is broken down in two

var r = print("foo") < this is the statement in the try block
return r

and then the content of the finally is executed before the return

var r = print("foo") < this is the statement in the try block
print ("ola")
return r

This is because the semantics of the try block is the following

  • run whatever is included in the try block. If it runs without errors, store the return value if present, then run the content of the finally block. If no errors occur here and we have a return value, just return that stored return value
  • if the try block has an error, then run what is in the except block (if present). If no errors here good, store the return value if present, the run the finalize block, If no errors here, just return the stored return value
  • if the except block as an error, the execution is stopped and the finally block is run. Then the exception is propagated to the outer block.
  • and now things can get messy, as you can (and will) nest multiple try blocks. The above mentioned steps repeat recursively until either the exception is handled or the interpreter try block is reached. In the latter case, the application is stopped and the exception is printed to the console with the usual traceback syntax.

This is just to give you an insight about how it works. Certain details might miss or might be not 100% correct. If you want to have more information, you can play with exceptions at W3C and learn more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM