正常運行代碼示例:
def finally_test():
try:
pass
print('i am in try start---')
# print(b)
print('i am in try after---')
return 'success'
except:
print('except error ---')
return 'error'
finally:
print("i am finally ---")
if __name__ == "__main__":
res = finally_test()
print("res = {}".format(res))
輸出:
i am in try start---
i am in try after---
i am finally ---
res = success
異常運行代碼示例:
def finally_test():
try:
pass
print('i am in try start---')
print(b)
print('i am in try after---')
return 'success'
except:
print('except error ---')
return 'error'
finally:
print("i am finally ---")
if __name__ == "__main__":
res = finally_test()
print("res = {}".format(res))
輸出:
i am in try start---
except error ---
i am finally ---
res = error