0
点赞
收藏
分享

微信扫一扫

python自定义异常类

SDKB英文 2022-01-25 阅读 80
#自定义异常类
class AgeError(Exception):
    def __init__(self,errorInfo):
        Exception.__init__(self)
        self.errorInfo =errorInfo
    def __str__(self):
        return  str(self.errorInfo)+'年龄错误!!'

age = int(input('输入一个年龄:'))
if age < 1 or age > 150:
    raise AgeError(age)
else:
    print('正常的年龄', age)

python 自定义异常类的用法

举报

相关推荐

0 条评论