class InputException(Exception): '''自定义的异常类''' def __init__(self,length,atleast): self.length = length self.atleast = atleast def test_except(): try: s = input("请输入:") if len(s) < 4: # raise引发一个自己定义的异常 raise InputException(len(s),3) except InputException as e: print("InputException:输入的长度是%d,长度至少应是%d"%(e.length,e.atleast)) else: print("输入数值为:{}".format(s))
test_except()