抛出异常
from pip._vendor.distlib.compat import raw_input
class ShortInputException(Exception):
'''你定义的异常类。'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = raw_input('请输入 --> ')
if len(s) < 3:
# raise引发一个你定义的异常
raise ShortInputException(len(s), 3)
except EOFError:
print ('/n你输入了一个结束标记EOF')
except ShortInputException, x:#x这个变量被绑定到了错误的实例
print('ShortInputException: 输入的长度是 %d,长度至少应是 %d'% (x.length, x.atleast))
else:
print ('没有异常发生.')
运行结果:
$ python raising.py
请输入 -->
你输入了一个结束标记EOF
$ python raising.py
请输入 --> --> ab
ShortInputException: 输入的长度是 2, 长度至少应是 3
$ python raising.py
请输入 --> abc
没有异常发生.
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
浙公网安备 33010602011771号