机器学习之路之python基础9

“”"
在程序开发过程中,如果对某些代码的实行不能确定可以增加语句来捕获异常,以下是完整的异常代码
“”"
try:
num=int(input(“请输入一个整数”))
result=8/num;
print(result)
except ZeroDivisionError:
#except 错误类型1就是用来捕获错误类型1这种异常的
print(“0不能做除数”)
except ValueError:
print(“请输入数字”)
except Exception as result:
#捕获 未知错误类型异常
print(“输入的是小数”)
else:
#没有异常执行的代码
print(1)
finally:
#不管有没有异常都会执行的代码
print(2)

猜你喜欢

转载自blog.csdn.net/weixin_43247522/article/details/84789935