Catching errors in Python

#1

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?

0 Likes

#2

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.

0 Likes

#3

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

0 Likes

#4

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?

0 Likes

#5

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

0 Likes

#6

Hi,
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

0 Likes

#7

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

try:
    # 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.
1 Like