Python Exception Handling – NotImplementedError

Sep 22, 2021 12:21:41 PM | Python Exception Handling – NotImplementedError

Struggling with a NotImplementedError in your code? In this blog, we'll go over what it is, how to raise it, and how to handle it.

Today, we're making our way through our in-depth Python Exception Handling series, and we’ll dive into the NotImplementedError in Python. The NotImplementedError is commonly raised when an abstract method is not implemented in a derived class.

Throughout this post, we’ll examine the NotImplementedError in more detail, starting with where it sits in the larger Python Exception Class Hierarchy. We’ll also look at raising this error, how to handle it, and how Airbrake can help you find this error quickly.

Hierarchy

Like most Python errors, the NotImplementedError derives from the BaseException, more specifically from the RuntimeError class. 

  • BaseException 
    • RuntimeError
      • NotImplementedError 

The NotImplementedError is, fortunately, a non-fatal error. It won’t stop your application from running, but it’s always best to clear errors before they cause issues. 

What is the NotImplementedError? 

According to Python, the NotImplementedError occurs when an abstract method lacks the required derived class to override this method, thus raising this exception.

Let’s break this down a bit. 

While it doesn’t provide abstract classes, Python allows you to use its module, Abstract Base Classes (ABC). Abstract classes are helpful because they create blueprints for other classes and establish a set of methods.

An Abstract Base Class includes one or more abstract methods (methods that have been declared but lack implementation). Subclasses, or child classes, are necessary to implement these abstract methods. 

To summarize, you’ll see the NotImplementedError two reasons: 

  1. When you need a derived class to override the abstract method 
  2. When the implementation is still missing

Now that you know what a NotImplementedError is and why it happens, here’s what you need to do to raise this exception.  

How to Raise a NotImplementedError Exception 

The NotImplementedError is part of Python’s BaseException, meaning it is a built-in exception. Like with all BaseExceptions, you can raise a NotImplementedError exception.

Raise the NotImplementedError like so:

It’s that simple. But imagine having to raise every exception in a similar fashion manually? It’s a time-consuming process. Save time and find Python errors more quickly with Airbrake Error Monitoring

How to Handle a NotImplementedError 

If you run into the NotImplementedError, the recommended way to handle it is to implement the abstract method for which the error is being raised. Because the NotImplementedError is user-defined, Python can't raise this error on its own. So, you'll need to raise it by a package you’re using or code your team wrote. Once you've raised it, you can then implement whatever's missing.

Let’s refer back to this screenshot: 

In its current form, you’ll raise a NotImplementedError. All you need to do to handle this error is implement the howl function by a subclass before it’s used.  

That’s it! 

Why Use Airbrake Error Monitoring

Want to be alerted about errors within your application? Airbrake Error Monitoring and Performance Monitoring provides real-time alerts about any Python errors in your code. Airbrake alerts will tell you: 

  • When an unhandled Python error occurs 
  • Where an error exists, right down to the line of code
  • The number of occurrences of an error
  • New deploys that introduce errors 
  • Data about an error via the aggregations' tab


See for yourself how powerful Airbrake is with a free dev account. For the first 30 days, you'll have access to all of Airbrake's paid features and unlimited error and performance monitoring.

Written By: Alexandra Lindenmuth