要求:
提示用户输入密码 如果长度小于8位 就抛出异常
代码:
def input_passwd():
# 1.提示用户输入密码
pwd = input('请输入密码:')
# 2.判断密码长度
if len(pwd) >= 8:
return pwd
# 3.如果<8 就主动抛出异常
print('主动抛出异常')
# a.创建异常对象
ex = Exception('密码长度不够,必须大于8位')
# b.主动抛出异常
raise ex
# 注意:只抛出二不捕获 代码会报错
try:
print(input_passwd())
except Exception as re:
print(re)
结果: