python 异常处理

时间:2020-07-21 01:00:47   收藏:0   阅读:95

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)
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!