Python Custom Exception Class

Jul 4, 2019
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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.