python异常处理
names = ['alex','jack']
data = {}
try:
data['name']
except KeyError as e:
print("没有这个key",e)
except Exception as e:
print("未知错误", e)
else:
print("一切正常")
finally:
print("不管有没有错,都执行")
自定义异常:
raise触发异常
class RogerException(Exception):
def __init__(self,msg):
self.message = msg
try:
raise RogerException('脑子短路')
except RogerException as e:
print(e)