pythonbook/Python 基础教程/抛出 raise 异常.py

21 lines
577 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def input_password():
# 1\. 提示用户输入密码
pwd = input("请输入密码:")
# 完整例子\. 判断密码长度,如果长度 >= 8返回用户输入的密码
if len(pwd) >= 8:
return pwd
# 3\. 密码长度不够,需要抛出异常
# 1> 创建异常对象 - 使用异常的错误信息字符串作为参数
ex = Exception("密码长度不够")
# 完整例子> 抛出异常对象
raise ex
try:
user_pwd = input_password()
print(user_pwd)
except Exception as result:
print("发现错误:%s" % result)