[QGIS-Developer] Catch Python exceptions in QGIS console

Jorge Gustavo Pereira Bastos Rocha jgr at di.uminho.pt
Thu Jun 13 07:10:45 PDT 2019


Hi devs,

Am I able to catch Python exceptions in Python console?

I'm trying to debug a DB Manager related issue [1] using pg_services. I have the following piece of code, that runs fine in my computer, but fails on any other computer, within a specific intranet.

import psycopg2
print (psycopg2.__version__)
try:
    connection = psycopg2.connect(service='pg_trabalho')
    cursor = connection.cursor()
    print ( connection.get_dsn_parameters(),"\n")
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("Connected to - ", record,"\n")
except:
    print( psycopg2.InterfaceError )
    print( psycopg2.OperationalError )

When I run it, QGIS crashes [2]. I would prefer to handle the exception myself. How can I catch the exception without crashing QGIS?

[1] https://github.com/qgis/QGIS/issues/30027
[2] http://blog.geomaster.pt/wp-content/uploads/2019/06/qgis-crash-python-try-except.jpg

Thanks,

J. Gustavo
-- 
Jorge Gustavo Rocha
Departamento de Informática
Universidade do Minho
4710-057 Braga
Tel: +351 253604480
Fax: +351 253604471
Móvel: +351 910333888
skype: nabocudnosor 


More information about the QGIS-Developer mailing list