使用的是pycharm,python版本3.9,想要定义一个输入长度不足的异常。
代码如下:
class ShortInputException(Exception):
def _int_(self,length,atleast):
self.length=length
self.atleast=atleast
try:
text=input('please enter the password:')
if len(text)<3:
raise ShortInputException(len(text),3)
except EOFError:
print('you have entered an end sign')
except ShortInputException as result:
print('ShortInputException:you entered:%d,the len must be at least:%d'%(result.length,result.atleast))
else:
print('no error happened')
错误如下:
please enter the password:1
Traceback (most recent call last):
File "/home/hcc/PycharmProjects/pythonProject/test2.py", line 8, in <module>
raise ShortInputException(len(text),3)
__main__.ShortInputException: (1, 3)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/hcc/PycharmProjects/pythonProject/test2.py", line 12, in <module>
print('ShortInputException:you entered:%d,the len must be at least:%d'%(result.length,result.atleast))
AttributeError: 'ShortInputException' object has no attribute 'length'