names = ['bzh','bob']
data = {}
names[3]
data['jack']
try:
names[3]
data['jack']
except KeyError as e:
print("没有这个key",e)
except IndexError as e:
print("列表操作错误",e)
except Exception as e:
print("未知错误",e)
else
print("一切正常")
finally
print("不管有没有错,都执行")
自定义异常
class BzhException(Exception)
def __init__(self,msg):
self.message = msg
#def __str__(self):
#return self.message
try:
raise BzhException('数据库连接不上')
except BzhException as e:
print (e)