说明
1、错误可以通过try和except句来处理。可能出错的句子放在try句子里。
2、如果出错,程序执行将转移到下一个except句子的开头。
实例
defspam(divd):
try:
print(42/divd)
exceptZeroDivisionError:
print('Errorinvalidargument')
spam(1)
spam(3)
spam(0)
spam(4)
打印结果:
42.0
14.0
Errorinvalidargument
10.5
以上就是python异常时的语句处理,希望对大家有所帮助。
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
上一篇