标签:判断 逻辑 cep error: span tar indexer 异常 err
print(‘start....‘)
[1,2,3][1000]
print(‘stop...‘)
# IndexError: list index out of range
# 异常的追踪信息
# 异常的类型
# 异常的内容
# 为了增强程序的健壮性,即便是程序运行过程中出错了,也不要终止程序
# 而是捕捉异常并处理:将出错信息记录到日志内
# if 1>3
# print("run...")
print(x)
# NameError: name ‘x‘ is not defined
l=[‘a‘,‘b‘]
l[2]
# IndexError: list index out of range
int(‘abc‘)
# ValueError: invalid literal for int() with base 10: ‘abc‘
dic={‘name‘:‘egon‘}
dic[‘age‘]
# KeyError: ‘age‘
class Foo:
pass
Foo.x
# AttributeError: type object ‘Foo‘ has no attribute ‘x‘
age=input(‘>>: ‘).strip() # 输入的只要不是数字就会出错
if age.isdigit():
age=int(age)
if age > 18:
print(‘猜大了‘)
elif age < 18:
print(‘猜大了‘)
else:
print(‘猜对了‘)
else:
print(‘必须输入数字‘)
# print(‘start...‘)
# try:
# # 有可能会抛出异常的代码
# dic={‘name‘:‘egon‘}
# dic[‘age‘]
# # 子代码1
# # 子代码2
# # 子代码3
# except IndexError as e:
# print(‘异常的信息: ‘, e)
# except NameError as e:
# print(‘异常的信息: ‘, e)
#
# else:
# print(‘如果被检测的子代码块没有异常发生,则会执行else的子代码‘)
# finally:
# print(‘无论被检测的子代码块有无异常发生,都会执行finally的子代码‘)
#
#
# print(‘end...‘)
print(‘end...‘)
print(‘start...‘)
try:
print(‘1111111111‘)
l = [‘aaa‘, ‘bbbb‘]
l[3] # 抛出异常IndexError,该行代码同级别的后续代码不会运行
print(‘2222222222‘)
xxx
print(‘33333333‘)
dic={‘a‘: 1}
dic[‘a‘]
except IndexError as e:
print(‘异常的信息: ‘, e)
print(‘end....‘)
print(‘start...‘)
try:
print(‘1111111111‘)
l = [‘aaa‘,‘bbbb‘]
# l[3] # 抛出异常IndexError,该行代码同级别的后续代码不会运行
print(‘2222222222‘)
xxx
print(‘33333333‘)
dic = {‘a‘: 1}
dic[‘a‘]
except IndexError as e:
print(‘异常的信息: ‘, e)
except NameError as e:
print(‘异常的信息: ‘, e)
print(‘end....‘)
print(‘start...‘)
try:
print(‘1111111111‘)
l = [‘aaa‘, ‘bbbb‘]
l[3] # 抛出异常IndexError,该行代码同级别的后续代码不会运行
print(‘2222222222‘)
xxx
print(‘33333333‘)
dic = {‘a‘: 1}
dic[‘aaa‘]
# except (IndexError, NameError) as e:
# print(‘异常的信息: ‘, e)
# except KeyError as e:
# print(‘字典的key不存在: ‘, e)
except Exception as e: # 万能异常
print(‘所有异常都可以匹配的到‘)
print(‘end....‘)
print(‘start...‘)
try:
print(‘1111111111‘)
print(‘2222222222‘)
print(‘33333333‘)
x
except Exception as e: # 万能异常
print(‘所有异常都可以匹配的到‘)
else:
print(‘没有异常====>‘)
print(‘end....‘)
print(‘start...‘)
try:
print(‘1111111111‘)
l = [‘aaa‘, ‘bbbb‘]
l[3] # 抛出异常IndexError,该行代码同级别的后续代码不会运行
print(‘2222222222‘)
xxx
print(‘33333333‘)
dic = {‘a‘: 1}
dic[‘aaa‘]
finally: # 不处理异常,无论是否发生异常都会执行finally的子代码
print(‘====》》》》》应该把被检测代码中回收系统资源的代码放到这里‘)
print(‘end....‘)
标签:判断 逻辑 cep error: span tar indexer 异常 err
原文地址:https://www.cnblogs.com/2722127842qq-123/p/12717561.html