Python: Create an exception for a refused database conection with pymysql
<p>Today I has the problem with a down database when a programmed script were running. I would like to create an exception when this happend again to send me an email (I already create the function to do it). But I'm failing in trying to create an exception for it.</p>

<pre><code>   import pymysql.cursors

    #Conection Settings....

    try:

       with connection.cursor() as cursor:
             # Read a single record
            sql =   "select  count(*) as total_invoices, " \
                    "sum(e.Montant_vente - e.Montant_tva_recup) as total_amount " \
                    "from  extrait_sapeig_stat e " \
                    "where e.montant_vente != 0 " \
                    "and e.numero_client = 1086 " \
                    "and e.activite != 11 " \
                    "and e.invoice_date = date_add(curdate(), Interval -1 DAY) " \
                    "and right(e.Numero_facture,7) not in (1182499)  " \


            print(sql)
            cursor.execute(sql)
            inv_counts_2 = cursor.fetchall()


    finally:
        connection.close()
</code></pre>

<p>Because you haven't defined an exception.</p>

<pre><code>   import pymysql.cursors

    #Connection Settings....

    try:

       with connection.cursor() as cursor:
             # Read a single record
            sql =   "select  count(*) as total_invoices, " \
                    "sum(e.Montant_vente - e.Montant_tva_recup) as total_amount " \
                    "from  extrait_sapeig_stat e " \
                    "where e.montant_vente != 0 " \
                    "and e.numero_client = 1086 " \
                    "and e.activite != 11 " \
                    "and e.invoice_date = date_add(curdate(), Interval -1 DAY) " \
                    "and right(e.Numero_facture,7) not in (1182499)  " \


            print(sql)
            cursor.execute(sql)
            inv_counts_2 = cursor.fetchall()
    except Exception as e:
        print (e)

    finally:
        connection.close()
</code></pre>

python mysql pymysql try-except