python中try..except语句的使用方法

python中try..except语句的使用方法

小编给大家分享一下python中try..except语句的使用方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

tryexcept的简单介绍

try:可能出现异常的代码

except:如果出现异常执行的代码

直接给大家代码演示,如何使用try..except语句案例:

问题:求两数之商

基本的处理

try:
    num01 = int(input("数1:"))
    num02 = int(input("数2:"))
    print("%d / %d = %.2f"%(num01,num02,num01/num02))
except:
print("异常!")

具体的处理:

import traceback
try:
    num01 = int(input("数1:"))
    num02 = int(input("数2:"))
    print("%d / %d = %.2f"%(num01,num02,num01/num02))
except:
    print("异常")
traceback.print_exc(file = open("./AppErrorLog.txt","w"))

看完了这篇文章,相信你对python中try..except语句的使用方法有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!