Finally, a use case for finally – Python Exception Handling

See Python: Tips and Tricks for similar articles.

With the help of Bruce Gordon, .NET guru, who gave me a great example that he used to use in his C# classes, I’ve come up with the following Python example, which I think provides a real-world use-case for finally:

import mysql.connector
from mysql.connector import errorcode

config = {
    "user": "username",
    "password": "pwd",
    "host": "127.0.0.1",
    "database": "demodb",
    "raise_on_warnings": True,
}
try:
    cnx = mysql.connector.connect(**config)
    print("OPENED CONNECTION TO DB")
    try:
        cursor = cnx.cursor()
        query = "select name, country from customers"
        cursor.execute(query)
        print("RAN QUERY")
    except:
        print("QUERY FAILED")
        raise
    else:
        for (name, country) in cursor:
            print("-", name, ":", country)
    finally:
        cnx.close()
        print("CLOSED CONNECTION TO DB")
except Exception as e:
    print("Something went wrong:", e)
else:
    print("MORE COOL STUFF TO HAPPEN")

If everything works correctly, this will output:

OPENED CONNECTION TO DB
RAN QUERY
--list of customer names and countries
CLOSED CONNECTION TO DB
MORE COOL STUFF TO HAPPEN

If the connection to the database fails (e.g., because of a bad password), it will output something like:

Something went wrong: 1045 (28000): Access denied for user 
'username'@'localhost' (using password: YES)

In this case, the nested finally clause does not run because the exception occurred in the outer try block. This is what we want. The connection to the database failed to open, so there’s no reason to try to close it.

If the query fails to execute (e.g., because of unknown column name), it will output something like:

OPENED CONNECTION TO DB
QUERY FAILED
CLOSED CONNECTION TO DB
Something went wrong: 1054 (42S22): Unknown column 'name' in 'field
list'

In this case, the finally clause does run because the exception occurred in the inner try block. The connection to the database is successfully made in the outer try block, but the query in the inner try block causes an exception, which we catch and report and re-raise. By re-raising the exception, we can do some more clean up or reporting in the outer except clause and we prevent the outer else clause from running.

I hope this makes sense and is helpful. If anyone has any thoughts on how it could be improved, please let me know.

As an aside, you can download the MySQL Connector/Python used in the example here.

Written by Nat Dunn. Follow Nat on Twitter.


Related Articles

  1. Fixing WebVTT Times with Python
  2. Using Python to Convert Images to WEBP
  3. Scientific Notation in Python
  4. Understanding Python’s __main__ variable
  5. Converting Leading Tabs to Spaces with Python
  6. Python Clocks Explained
  7. Python: isdigit() vs. isdecimal()
  8. Maximum recursion depth exceeded while calling a Python object
  9. When to use Static Methods in Python? Never
  10. Finally, a use case for finally – Python Exception Handling (this article)
  11. Python Coding Challenge: Two People with the Same Birthday
  12. How to Create a Simple Simulation in Python – Numeric Data
  13. Collatz Conjecture in Python
  14. Associate Python Files with IDLE
  15. How to find all your Python installations on Windows (and Mac)
  16. Python Color Constants Module
  17. A Python Model for Ping Pong Matches
  18. Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
  19. pow(x, y, z) more efficient than x**y % z and other options
  20. Python’s date.strftime() slower than str(), split, unpack, and concatenate?
  21. Bi-directional Dictionary in Python
  22. Creating an Email Decorator with Python and AWS
  23. Change Default autosave Interval in JupyterLab
  24. Basic Python Programming Exercise: A Penny Doubled Every Day
  25. Simple Python Script for Extracting Text from an SRT File
  26. Python Virtual Environments with venv
  27. Mapping python to Python 3 on Your Mac
  28. How to Make IDLE the Default Editor for Python Files on Windows
  29. How to Do Ternary Operator Assignment in Python
  30. How to Convert Seconds to Years with Python
  31. How to Create a Python Package
  32. How to Read a File with Python
  33. How to Check the Operating System with Python
  34. How to Use enumerate() to Print a Numbered List in Python
  35. How to Repeatedly Append to a String in Python
  36. Checking your Sitemap for Broken Links with Python
  37. How to do Simultaneous Assignment in Python
  38. Visual Studio Code - Opening Files with Python open()
  39. How to Slice Strings in Python
  40. How Python Finds Imported Modules
  41. How to Merge Dictionaries in Python
  42. How to Index Strings in Python
  43. How to Create a Tuple in Python