python 异常处理


name = ["test","测试"]
data = {}


try:
# open("oooo.txt")
# name[3]
# data["name"]
a = 1
print(a)

except (KeyError,IndexError) as e:
print("没有这个key", e)

except KeyError as e:
print("没有这个key",e)
except IndexError as e:
print("列表操作错误",e)
except Exception as e:#抓取所有错误
print("未知错误",e)
else :
print('一切正常')

finally:
print('不管有没有错,都执行')


#自定义异常
class WupeiqiException(Exception):

def __init__(self, msg):
self.message = msg

def __str__(self):
return self.message
try:
raise WupeiqiException('我的异常')
except WupeiqiException as e:
print(e)
posted @ 2020-07-20 21:27  安好_世界  阅读(121)  评论(0编辑  收藏  举报