python异常处理

try  except  finnally

#!/usr/bin/env python
#coding=utf-8
try:
while True:
a=raw_input("please input a number:")
b=raw_input("please input another number:")
print(int(a)/int(b))
except ZeroDivisionError,e:
print("除数为0,请重新输入")
except ValueError,e:
print("请输入整数")
except:
print("未知异常错误")


基本格式就是前面是异常的 对象,后面e 是保存异常的原因
这么弄的好处是捕获到异常,抛出异常能让使用程序人员清晰知道异常是因为什么原因产生的。
类似数据库方面的异常情况。在无法捕获情况下 弄一个未知异常,避免解释器抛错。
原文地址:https://www.cnblogs.com/chenxiaoyong/p/6212988.html