class CustomException(Exception): def __init__(self, code, msg=None): # msg=None as hack for pickle issue self.code = code self.msg = msg message = f"[{self.code}] {self.msg}" # Python 2.x # super(CustomException, self).__init__(message) super().__init__(message)
Usage
raise CustomException(code=100, msg="Test")
References: