class AgeError(Exception):
def __init__(self,error_info:str):
self.error_info = error_info
def __str__(self):
return self.error_info
#测试
age = int(input("请输入年龄:"))
try:
if age<0 or age>200:
raise AgeError("年龄不在范围内")
else:
print("年龄为:",age)
except AgeError as e:
print(e.error_info)
python自定义异常类
最新推荐文章于 2024-08-22 22:01:15 发布