简体   繁体   中英

raise Exception not catching exception as expected with two functions

Here is the Code:

def funcFail():
    try:
        raise Exception("Failed externally")
        print("Should not print")
    except Exception as e:
        print(f"Exception : {e}")
        raise Exception(f"Exception in occured due: {e}")
    finally:
        return "This is finally block :P"
        print("This is finally block :P")

def checkexpcep():
    global k 
    k = []
    try:
        funcFail()
    except Exception as e:
        print(f"Exception out : {e}")
        k.append(str(e))
    finally:
        return "nothing"

checkexpcep()

Expected:

"Exception : Failed externally"
"This is finally block :P"
"Exception out : Exception in occured due: Failed externally"

Output:

"Exception : Failed externally"

next time try to use python debugger (any ide is good enough, pycharm or wing,..etc). anyway see my inline comment numbers in your code below:

def funcFail():
    try:
        # (2)
        raise Exception("Failed externally")
        print("Should not print")
    except Exception as e:
        print(f"Exception : {e}")
        # (3)
        raise Exception(f"Exception in occured due: {e}")   
    finally:
        # (4)
        return "This is finally block :P"
        print("This is finally block :P")

def checkexpcep():
    global k 
    k = []
    try:
        # (1)
        funcFail()
        # (5)
    except Exception as e:
        print(f"Exception out : {e}")
        k.append(str(e))
    finally:
        return "nothing"

checkexpcep()

after calling checkexpcep() you reach point 1 in the code. then you call funcFail() and reach point 2 , at 2 you are raising an exception which cactched by the except block and you reach point 3 with this exception, after printing the exception, you are raising new exception at point 3 , and you reach the finally block and python execute it (be aware, finally always executed). you are returning from finally block with the string "This is finally block:P" .this shallow or hides the previous exception. the return brings you to point 5 . nothing is done with the return value and the program is finished succefully.

If you actually want your expected output, then just move the return out of the finally block:

def funcFail():
    try:
        raise Exception("Failed externally")
        print("Should not print")
    except Exception as e:
        print(f"Exception : {e}")
        raise Exception(f"Exception in occurred due: {e}")
    finally:
        print("This is finally block :P")
    return "This is finally block :P"

def checkexpcep():
    global k 
    k = []
    try:
        funcFail()
    except Exception as e:
        print(f"Exception out : {e}")
        k.append(str(e))
    finally:
        return k,"nothing"

print(checkexpcep())

Output:

Exception : Failed externally
This is finally block :P
Exception out : Exception in occurred due: Failed externally
(['Exception in occurred due: Failed externally'], 'nothing')

(Note I added the print of a tuple at the end to show the value of k )

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