【Python】Exception

Posted by 西维蜀黍 on 2019-10-20, Last Modified on 2024-01-09

try-except

try:
    raise FError("自定义异常")
except FError as e:
    print(e)

自定义异常

class ObjectNotExistInDBError(Exception):
    def __init__(self,msg):
        super(ObjectNotExistInDBError,self).__init__(self)
        self.msg=msg
    def __str__(self):
        return self.msg

抛出异常

主动抛出异常:

raise RuntimeError('testError')