python 异常获取方法
2021-06-11 10:03
标签:star color ace NPU imp 直接 call visio rac 结果: ============================================================================= RESTART: D:/Python/Python37/file2.py ============================================================================ 第5 python 异常获取方法 标签:star color ace NPU imp 直接 call visio rac 原文地址:https://www.cnblogs.com/xkdn/p/14225343.htmlimport sys
#第1:print(6/0) #直接运行该命令,出现异常,程序终止
#异常提示:
‘‘‘Traceback (most recent call last):
File "F:/file2.py", line 2, in
第2
division by zero
第3
Traceback (most recent call last):
File "D:/Python/Python37/file2.py", line 21, in
print(5/0)
ZeroDivisionError: division by zero
第4
Traceback (most recent call last):
File "D:/Python/Python37/file2.py", line 29, in
print(5/0)
ZeroDivisionError: division by zero
找到了
division by zero
>>>aa=int(input("请输入aa的值:"))
bb=int(input("请输入bb的值:"))
try:
answer=aa/bb
except ZeroDivisionError:
print("0不能做除数")
else:
print(answer)
#依赖try代码块成功执行的代码都应放到else代码块中
#如果没有else则下面的代码利用得都是try成功执行的结果继续运行,否则会except提示