python脚本应用中的检测:处理异常和触发异常

#!/usr/bin/python
#coding:utf-8
try:
x=int(raw_input("请输入用户名:"))
print 3/x
except KeyboardInterrupt:
print "你输入了crtl+c,请重试"
except EOFError:
print "你输入了ctrl+d,请重试"
except ValueError:
print "你输入的不是数字"
except ZeroDivisionError,e: #把自带报错存入变量
print "除数不能输入0:",e #打印自带报错信息
else:
print "没毛病" #不出错执行
finally:
print "Game Over" #出不出错都执行
python脚本应用中的检测:处理异常和触发异常

#!/usr/bin/python
#coding:utf-8
x=raw_input("xxx你爱我吗(y/n)?")
try:
if x!='y':
raise ValueError,"答案不对,请重试"
except ValueError,e:
print e
else:
print "OK"