Python-try except else finally有return时执行顺序

http://stackoverflow.com/questions/19805654/python-try-finally-block-returns

 

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

 

posted @ 2016-08-31 17:24  blue_whale  阅读(259)  评论(0)    收藏  举报