【Python】自定義Error或者Exception
阿新 • • 發佈:2019-02-23
print ndpi clas gitlab catch super http def self 為什麽需要自定義異常?
因為需要花式catch exception
代碼
class MyException(Exception): def __init__(self, arg1, arg2): super(MyException, self).__init__(arg1, arg2) self.arg1 = arg1 self.arg2 = arg2 class MyException(Exception): def __init__(self, message, level=‘info‘): super(MyException, self).__init__(message, level) self.message = message self.level = level if __name__ == ‘__main__‘: try: raise PortNotFoundPidError(‘fuck u‘, level=‘emergent‘) except PortNotFoundPidError as e: print(e) print(‘Error message:{} \nError level:{}‘.format(e.message, e.level))
/usr/local/bin/python2.7 /Users/psyduck/GitLab/monitor/scripts/Errors.py
##### output #####
# (‘fuck u‘, ‘emergent‘)
# Error message:fuck u
# Error level:emergent
參考連接
為什麽要super(MyException, self).__init__()
?
【Python】自定義Error或者Exception