Common Python exceptions: how to discover and fix problems in your Python codePosted Apr 23, 2015 | 3 min. (590 words)
_*This article was last updated July 2017 _
It’s time to look at common Python exceptions that occur frequently in Python code. (Plus how to fix them.)
Python exceptions are well supported and have been since Python 1.5 and the introduction of proper exception objects. (Replacing string exceptions which were deprecated in 2.5.) Python 2.6 adds the ‘as’ syntax when one or more (as a tuple) exception types can be defined to be caught by a certain
except block. This makes it simple to port 2.x code to Python 3.
The following exceptions are subclasses of Exception and are included in the standard library, which you can subclass yourself to your types. They are also descriptive of the cause in your code. Subclasses are used as part of the identity we create when grouping exceptions intelligently, among other techniques.
Without more ado, here’s a list of python exceptions you’ll frequently encounter, and how Raygun’s Python provider can assist in debugging them.
This exception represents the action of referencing or assigning to an attribute that doesn’t otherwise exist – the classic null reference. Certain parameters are expected to have a value in the happy case will frequently not at runtime, which causes this exception.
The official Raygun provider for Python, Raygun4py, will present the backtrace that resulted in the AttributeError. The error message will tell you what attribute was the cause, and the upcoming local variables feature will enable you to see the exact state in each frame. This allows you to pinpoint the cause of the error right after you are notified by email.
The exception ValueError is by a function when one of its parameters is an invalid value. An example is divide (numerator, denominator) where 0 is passed in as the second param. As above you can see the offending variable in the error message in the Raygun dashboard, and its value in the local variables throughout the stack.
TypeError is a very common exception and gets thrown when an individual operation is performed on an unexpected type. If this occurs in a web context due to malformed user input (especially critical in a REST API), the exception gets buried within a log file. Using a tool like Raygun will ensure you are notified promptly and have all the data you need to debug the exception and push a fix quickly.
When unit testing, AssertErrors will be raised by the unittest(2) module when a unit test fails. Your unit tests may run on a build server (or service like Travis). Being notified by email that a build has failed often works well. Some tooling can give you a massive text dump of the build log. This is time-consuming to sift through as tests fail.
Raygun allows you to extend this pattern by sending exceptions as well as raising them during the build process. You get notified by the exact tests that fail with their stack trace. In combination with the deployments feature (with GitHub integration for viewing the actual source code) visualizing regressions between builds is easy and allows you to pinpoint how committed code may have broken the build.
Discover Python exceptions faster
If you’d like to integrate a world-class exception tracking solution into your Python code, start your 14-day free trial of Raygun. Got questions? Let us know in the comments below, and we’ll get back to you pronto. We have great support from real humans.
Until next time, keep frying those exceptions!
Architecting software errors for better error reporting