python 异常处理

2021-04-12 21:27

阅读:581

标签:没有   自定义异常   except   表操作   class   def   int   操作   data   


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)

python 异常处理

标签:没有   自定义异常   except   表操作   class   def   int   操作   data   

原文地址:https://www.cnblogs.com/anhao-world/p/13347915.html


评论


亲,登录后才可以留言!