Catching errors in Python

I’m trying to catch any possible errors arising from a SQL UPDATE (such as a duplicate key value) so that I can display a custom error message. I’ve tried the following codetry: runUpdateQuery(blah) except: print "Error" without success - even when the update fails the standard warning appears and ‘Error’ never gets printed in the console.

Is what I’m trying to do possible?

Not currently, no. fpmi.db.runUpdateQuery catches errors internally and displays them. I’ve taken the liberty of moving this thread to the feature request forum.

This has been changed in Ignition - all database errors can now be caught and handled via Python exception handling.

That’s great :thumb_left:

Just to prove users are never satisfied - is there any way you can trap a SQL error number from the database?

You mean how to pull out details of the error that you’ve caught? You can catch the exception and learn about its cause like this:

import sys from java.lang import Exception try: system.db.runQuery("SELECT something FROM nothing") print 'Success!' except Exception, errinfo: print 'Error running query: ', errinfo.cause.message

I am trying in my except block:

except Exception, errorinfo:

But the exception is not being caught up. I am running 7.9.6 (b2018012914)
On the log it says:
Caused by: com.inductiveautomation.ignition.gateway.datasource.FaultedDatabaseConnectionException: The database connection ‘scia’ is FAULTED. See Gateway Status for details.
Any ideas are appreciated

Java exceptions are not subclasses of python’s Exception class, so your except class is excluding the java exceptions.

Try code something like this:

import java.lang.Exception

    # some code that can throw either jython or java exceptions
except java.lang.Exception, e:
    # handle the java exception in e.  You might need to use it's cause property to access nested exceptions
except Exception, e:
    # handle a python exception as usual.