说明

1、错误可以通过try和except句来处理。可能出错的句子放在try句子里。
2、如果出错,程序执行将转移到下一个except句子的开头。
实例
def spam(divd):
    try:
         print(42 / divd)
    except ZeroDivisionError:
        print('Error invalid argument')
 
spam(1)
spam(3)
spam(0)
spam(4)
 
打印结果:
42.0
14.0
Error invalid argument
10.5
以上就是python异常时的语句处理,希望对大家有所帮助。更多Python学习指路:创新互联Python教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号