How to create a user-defined exception that changes an already existing method inherited from a call?

32 Views Asked by At

I am trying to create an exception that changes a methods print message from "meow" to "Nope, scratch scratch"; but not sure where this is going wrong?

    def __init__(self, name):
        self.name = name
    
    def meow(self):
        print("meow")

class GoodCat(Cat):
    def __init__(self, name):
         super().__init__(name)

    def meow(self):     
        print("purr purr meow")
    
class BadCat(Cat):
    def __init__(self, name):
        super().__init__(name)

    def meow(self):
        print("hiss")
    
class VeryBadCat(Cat):
    def __init__(self, name):
        super().__init__(name)

class MyException(Exception):
    pass
    def meow(self):
        try:
            if super().meow():
                return True
            raise MyException("Nope, scratch scratch")
        except MyException as e:
            print(e)
        finally:
            print("Program Stop")

Would appreciate an explanation with the answer please; as I have at this for some time now and simply, licking, and sticking my finger in the air at this point.

0

There are 0 best solutions below