Python NotImplementedError Exception
Example
A method that child classes must override:
class Animal:
def sound(self):
raise NotImplementedError("Child classes must create sound()")
class Dog(Animal):
pass
dog = Dog()
dog.sound()
Try it Yourself »
Definition and Usage
The NotImplementedError exception is used in a method that a child class must override (replace with its own version).
In the example, Dog does not have its own sound() method, so the method from Animal runs and raises the error.
Exception Handling
Example
Handling the NotImplementedError in a try...except statement:
class Animal:
def sound(self):
raise NotImplementedError("Child classes must create sound()")
class Dog(Animal):
pass
dog = Dog()
try:
dog.sound()
except NotImplementedError as e:
print("Error:", e)
Try it Yourself »
Related Pages
Read more about handling errors in our Python Try Except Tutorial.