Python异常处理

Posted Kimisme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python异常处理相关的知识,希望对你有一定的参考价值。

1.try…except…else

try:
    fn = open("aa.txt","r")
    fn.write("这是一个测试文件")
except Exception as e:
    print "错误提示",e.message
else:
    print "写入内容成功"
    fn.close()

2.try…finally

try:
    fn = open("aa.txt","r")
    fn.write("这是一个测试文件")
except Exception as e:
    print "错误提示",e.message
finally:
    print "我都会执行"
    fn.close()

以上是关于Python异常处理的主要内容,如果未能解决你的问题,请参考以下文章