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

**URL:** <https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695>\
**Category:** Ignition\
**Created:** [February 28, 2020, 2:03pm UTC](https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695 "2020-02-28T14:03:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [February 28, 2020, 2:03pm UTC](https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695/1 "2020-02-28T14:03:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 28, 2020, 2:15pm UTC](https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695/2 "2020-02-28T14:15:40Z")

</div>

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](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:

> [@Catch IOException?](http://forum.inductiveautomation.com/t/catch-ioexception/18612/3):
>
> Consider using two except blocks, as jython’s standard Exception won’t catch pure java exceptions. Like so: import java.lang.Exception try: # something something something except Exception, e: # jython exception except java.lang.Exception, e: # java exception

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 28, 2020, 2:22pm UTC](https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695/3 "2020-02-28T14:22:22Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [February 28, 2020, 2:50pm UTC](https://forum.inductiveautomation.com/t/solved-gracefully-exit-a-system-db-runprepquery-or-system-db-runprepupdate-script-statement/32695/4 "2020-02-28T14:50:32Z")

</div>

Thank you, this is the answer I needed.
