A conversation about exceptions


Other person: The subclasses of this Foo parent class are supposed to define method bar(). So we should define Foo.bar() and have it raise an exception.

Me: Why?

Other person: So if subclass_instance.bar() is called, and the subclass hasn’t overridden bar(), it’ll raise an exception. That way we’ll know when the method is missing on a subclass!

Me: If we don’t define bar() in the parent class, what happens if a subclass also doesn’t define it, and code calls subclass.bar()?

Other person: It raises an exception.

Me:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.