Member-only story

A Comprehensive Guide to Handling Exceptions in Python

The dos and don’ts of best-practice Python exception handling

Henry George
Better Programming
13 min readJun 3, 2020

--

Photo by ConvertKit on Unsplash

No one wants their code throwing errors, but exceptions in Python can have a whole variety of use cases and are critically important to writing good code.

This article describes some examples of what to do and not do when writing exceptions. Hopefully, it’ll instill in you a deeper understanding of exception handling broadly as well provide some useful Python tips.

Do Write Lots of Exceptions

  • AttemptedToAccessCoreDatabasePriorToDatabaseTransactionBeingInitiatedException
  • JavascriptMillisecondTimeFormatBufferOverflowException
  • CannotDeleteUserAccountThatDoesntExistException

Exceptions, exceptions, exceptions everywhere. These names look challenging to read and are long to type, but are they really so scary?

These exceptions provide the most valuable function exceptionally well — they’re specific, informative, and to the point.

OK, the last one could probably be renamed to CannotDeleteNonExistentUser, sure, but the point is they’re clear. You know where to go to find the issue, and you know exactly what to try except if you’re OK with…

--

--

Henry George
Henry George

Written by Henry George

Entrepreneur, Developer, Mathematician. Artificial Intelligence, Startups and Software Development.

Responses (4)

What are your thoughts?