1,简单异常处理
try:
date["name"]
names[3
]
except (IndexError, KeyError) as e:
print(
'检测IndexError/KeyError异常', e)
except Exception as e:
print(
'检测所有异常', e)
else:
print(
'一切正常')
finally:
print(
'有错无措,都执行')
2,自定义异常处理
# 自定义异常类,继承Exception
class ElvaException(Exception):
def __init__(self, msg):
self.message =
msg
def __str__(self):
return '测试测试'
# return self.message
try:
raise ElvaException(
'数据库连接错误')
# raise调用自己定义异常
except ElvaException as e:
print(e)
转载于:https://www.cnblogs.com/Linc2010/p/9011640.html
转载请注明原文地址: https://mac.8miu.com/read-69566.html