[SOLVED] Gracefully exit a system.db.runPrepQuery or system.db.runPrepUpdate script statement

Is there a way to gracefully exit a function if some type of SQL statement fails such as runPrepQuery or runPrepUpdate? I am getting errors sometimes due to database primary keys/foreign key relationships and I don’t want to “exit” the function, I just want the statement to be skipped over.

Or is this happening because my design is bad?

Use a try - except code block. Catching exceptions is a standard feature of python, like pretty much any modern programming language.

https://docs.python.org/2.7/reference/compound_stmts.html#the-try-statement

Note that if you specify the generic python Exception class in your except clause, you won't catch java-specific exceptions. You would need an extra except clause:

2 Likes

Just to go off what pturmel said, if you are getting primary key/foreign key relationship and otherwise integrity errors, those ARE java errors which will need a specific catch.

import java.lang.Exception
try:
    system.db.runPrepUpdate("Some Query",[value1, value2,...])
except Exception, e:
    print "python exception : " + str(e)
except java.lang.Exception, e:
    print "java exception: " + str(e)

However, if you are getting errors with foreign keys/primary keys, I would also take another look at your database design and see what is going on. Some constraints were obviously put on the tables for data integrity reason and it will only help your code to know what they are/why certain things are failing.

2 Likes

Thank you, this is the answer I needed.