发布时间:2019-08-02 11:14:57编辑:auto阅读(2143)
异常时程序发生错误的信号,一旦程序出错机会产生一种类型的异常,
为了保证程序的健壮性
语法错误:程序运行前就应该解决
逻辑错误:try…except
异常处理一:
try:
print(x)
print('hello')
l=[1,2,3]
l=[0]
except NameErroras e:
print('按照这个方法处理')
print('other code...')
异常处理二:
try:
# print(x)
print('hello')
l=[1,2,3]
l=[0]
dic={'x':111}
dic['e']
except NameErroras e:
print('按照这个方法处理')
except IndexErroras e:
print('index error')
except KeyErroras e:
print('key error')
异常处理三:
try:
# print(x)
print('hello')
l=[1,2,3]
l=[0]
dic={'x':111}
dic['e']
except (NameError,IndexError,KeyError)as e:
print(e)
异常处理四:
try:
# print(x)
print('hello')
l=[1,2,3]
l=[0]
dic={'x':111}
dic['e']
except Exceptionas e:
print(e)
异常处理五:
try:
# print(x)
print('hello')
l=[1,2,3]
l=[0]
dic={'x':111}
dic['e']
except NameErroras e:
print('name error')
except IndexErroras e:
print('index error')
except Exceptionas e:
print(e)
异常处理六:
try:
# print(x)
print('hello')
l=[1,2,3]
l=[0]
dic={'x':111}
dic['e']
except NameErroras e:
print('name error')
except IndexErroras e:
print('index error')
except Exceptionas e:
print(e)
else:
print('当被检测代码块没有异常时执行')
finally:
回收系统资源
上一篇: python元祖
下一篇: python 图片识别
52033
51888
42125
38971
33471
30422
29054
24082
23998
22345
56°
99°
125°
113°
130°
124°
149°
164°
163°
168°