python 自定义异常

python2

#coding=utf-8
class CustomError(Exception):
    def __init__(self,ErrorInfo):
        self.errorinfo=ErrorInfo

    def __str__(self):
        return self.errorinfo

if __name__ == '__main__':
    try:
        raise CustomError('客户异常')
    except CustomError as e:
        print(e)

 

python3 

#coding=utf-8
class CustomError(Exception):
    def __init__(self,ErrorInfo):
        super().__init__(self) #初始化父类
        self.errorinfo=ErrorInfo

    def __str__(self):
        return self.errorinfo
    
if __name__ == '__main__':
    try:
        raise CustomError('客户异常')
    except CustomError as e:
        print(e)

 

posted @ 2019-04-19 00:05  anobscureretreat  阅读(173)  评论(0编辑  收藏  举报