#try…except…
#捕获分母为0的错误,并打印错误
#try可以加多个except
try:
var=float(input(“请输入一个数字”))
print(var/0)
except ZeroDivisionError as e:
print(“分母不能为零”,e)
except ValueError as e:
print(“值错误”,e)
print(2/1)
#使用Exception代指所有异常
try:
var=float(input(“请输入一个数字”))
print(1/var)
except Exception as e:
print(“出现异常:”,e)
print(2/1)
#finally,最终执行的操作
try:
var=float(input(“请输入一个数字”))
print(1/var)
except Exception as e:
print(“出现异常:”,e)
finally:
print(“程序执行完毕!”)
print(2/1)
#raise 跑出异常
try:
var=float(input(“请输入一个数字”))
print(1/var)
except Exception as e:
print(“出现异常:”,e)
raise
finally:
print(“程序执行完毕!”)
print(2/1)