9 Python Exception Things I Regret Not Knowing Earlier

Liu Zuo Lin
Level Up Coding
Published in
8 min readSep 7, 2024

--

1) The exception hierarchy

Python has a number of built-in Exceptions that we might meet from time to time eg. ZeroDivisionError, KeyError, ValueError, TypeError and so on.

Each of these exceptions are part of an Exception hierarchy — which means that most of these exceptions inherit from the same parent class Exception one way or another.

One way to see this for ourselves is to print the subclasses of some exception class using .__subclasses__()

Another way is the use .__bases__ to check an exception’s parent classes

This might be a hassle, so I’ve created a function to help you automate this:

Some examples:

--

--