1. class ShortInputError(Exception):
    2. # 自定义异常
    3. def __init__(self, length, atleast):
    4. self.length = length
    5. self.atleast = atleast
    6. try:
    7. text = input('请输入密码:\n')
    8. if len(text) < 3:
    9. raise ShortInputError(len(text), 3)
    10. except ShortInputError as result:
    11. print(f"输入的长度是{result.length},长度至少应该是{result.atleast}")
    12. else:
    13. print("成功")