发布时间:2019-08-02 11:14:57编辑:auto阅读(2018)
异常时程序发生错误的信号,一旦程序出错机会产生一种类型的异常,
为了保证程序的健壮性
语法错误:程序运行前就应该解决
逻辑错误: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 图片识别
 51195
 50614
 41237
 38052
 32515
 29421
 28282
 23137
 23096
 21434
 1490°
 2203°
 1829°
 1758°
 2066°
 1816°
 2500°
 4198°
 4060°
 2896°