Search code examples
pythonimpalaimpyla

How to handle exception in the "finally" block?


Given the following Python code:

# Use impyla package to access Impala
from impala.dbapi import connect
import logging

def process():
    conn = connect(host=host, port=port)  # Mocking host and port
    try:
        cursor = conn.cursor()
        # Execute query and fetch result
    except:
        loggin.error("Task failed with some exception")
    finally:
        cursor.close()  # Exception here!
        conn.close()

The connection to Impala was created. But there was an exception in cursor.close() due to Impala timeout.

What is the proper way to close the cursor and conn given the latent exception?


Solution

  • You have to nest the try-blocks:

    def process():
        conn = connect(host=host, port=port)  # Mocking host and port
        try:
            cursor = conn.cursor()
            try:
                # Execute query and fetch result
            finally:
                # here cursor may fail
                cursor.close()
        except:
            loggin.error("Task failed with some exception")
        finally:
            conn.close()
    

    To avoid such try-finally-blocks, you can use the with-statement:

    def process():
        conn = connect(host=host, port=port)  # Mocking host and port
        try:
            with conn.cursor() as cursor:
                # Execute query and fetch result
                # cursor is automatically closed
        except:
            loggin.error("Task failed with some exception")
        finally:
            conn.close()