异常处理 day07-09

l = [1,2,3]
d = {"name":"11"}
import traceback
try:
name = d["name"]
# l[4]
# except KeyError as e:
# print(e)
# print("出现异常了")
# except IndexError as e:
# print("出现下标不存在的异常了")
except Exception as e:
traceback.print_exc() # 打印报错哪一行的
msg = traceback.format_exc() # # 打印报错哪一行的 可写入文件里面
print(msg)
else:
print("没有出现异常")
finally:
print("不管你出不出异常他都会走")
上一篇:python 异常


下一篇:Python 异常处理-Python零基础入门教程