10.1 Python标准异常类
10.2 实验
In:
a = 1
print(a + "输出")
out:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-6e1f6d473de4> in <module>
1 a = 1
----> 2 print(a + "输出")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In:
try:
a = 1
print(a + "输出")
except Exception as e:
print(f'{a}类型不是字符,错误代码:{e}')
out:
1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'
In:
def prt_info(info):
try:
print(info + "输出")
except Exception as e:
print(f'{info}类型不是字符,错误代码:{e}')
else:
print("正常")
In:
prt_info(1)
out:
1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'
In:
prt_info('a')
out:
a输出
正常
In:
def prt_info2(info):
try:
print(info + "输出")
except Exception as e:
print(f'{info}类型不是字符,错误代码:{e}')
finally:
print("执行完成")
In:
prt_info2(1)
out:
1类型不是字符,错误代码:unsupported operand type(s) for +: 'int' and 'str'
执行完成
In:
prt_info2('a')
out:
a输出
执行完成