发布时间:2019-08-02 11:14:57编辑:auto阅读(1461)
异常时程序发生错误的信号,一旦程序出错机会产生一种类型的异常,
为了保证程序的健壮性
语法错误:程序运行前就应该解决
逻辑错误: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 图片识别
47570
45930
36870
34434
29044
25681
24532
19687
19207
17718
5542°
6121°
5654°
5710°
6666°
5450°
5456°
5961°
5936°
7268°