Search code examples
pythonclassmethodscustom-exceptions

Can i define methods in exception classes other than __init__ in Python?


As the question already asks, I'm aware of building a custom exception class like this:

class MyException(Exception) :

   def __init__(self, message='My error message') :
       self.message = message
       super(MyException, self).__init__(message)

which will build a custom Exception class object with a predefined overwritable message.

I wanted to know if it's possible to define more methods in it, other than __init__, and what would the effects of that be.

Thanks


Solution

  • Yes, you can. It won't have any effect other than having those other methods on it.