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:
after calling
checkexpcep()you reach point1in the code. then you callfuncFail()and reach point2, at2you are raising an exception which cactched by the except block and you reach point3with this exception, after printing the exception, you are raising new exception at point3, and you reach thefinallyblock 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 point5. nothing is done with the return value and the program is finished succefully.