# How to catch exceptions from database calls such as system.db.runPrepUpdate

**URL:** <https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [October 14, 2022, 7:41pm UTC](https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248 "2022-10-14T19:41:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Webb](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@Daniel\_Webb](https://forum.inductiveautomation.com/u/Daniel_Webb)\
**Post date:** [October 14, 2022, 7:41pm UTC](https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248/1 "2022-10-14T19:41:46Z")

</div>

I'm having trouble understanding how to comprehensively catch exceptions thrown while doing database updates. Here is an example where I tried to INSERT a string which could not be converted to a float into a column of type 'float', so obviously it threw an exception. Here is a part of it:

```auto
Caused by: org.python.core.PyException: java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO TestTable (val) VALUES (?), [3z], XOM, , false, false)
	... 56 common frames omitted
Caused by: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO TestTable (val) VALUES (?), [3z], XOM, , false, false)
	... 55 common frames omitted
Caused by: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: SQL error for "INSERT INTO TestTable (val) VALUES (?)": Error converting data type nvarchar to float.

```

I'm especially interested in the last line since it tells me the real cause, as opposed to the java.lang.Exception, which doesn't tell me WHY the call failed. I wrap a lot of my code in exception catchers, and when I re-raise the exception the GatewayException is stripped out and I don't see it in the re-raised exception. Here's a simplified version of what I'm talking about:

```auto
import sys
anInt = '3z'
try:
	system.db.runPrepUpdate("INSERT INTO TestTable (val) VALUES (?)", [anInt], 'XOM')
except:
	errorType,value,trace = sys.exc_info()
	raise errorType, value, trace # re-raise

```

which re-raises the java.lang.Exception part (so I see it in the console), but not the GatewayException part. What is the magic import and except clause which will allow me to catch the GatewayException?

---

<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:** [October 14, 2022, 7:57pm UTC](https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248/2 "2022-10-14T19:57:24Z")

</div>

See this topic:

> [@Generic Python error handler?](https://forum.inductiveautomation.com/t/generic-python-error-handler/60584):
>
> I’m trying to code a generic type of error handler that I can call from the except clauses from different scripts where the error\_handler procedure can take different routes depending on what is passed to it. Ideally, I want a generic error handler that will catch any/all types of errors. My question is, will the str(e) in the except clause below work correctly for all or most types of errors? try: i = 1 / 0 # divide by zero error except Exception as e: error\_handler(str(e)) # wil…

---

<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:** [October 14, 2022, 8:00pm UTC](https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248/3 "2022-10-14T20:00:09Z")

</div>

Also, note that java exceptions are allowed to have a "cause" attached, and that one can have a cause, et cetera. Look at `e.cause`.

---

<div class="post-metadata">

**Author:** ![Daniel\_Webb](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@Daniel\_Webb](https://forum.inductiveautomation.com/u/Daniel_Webb)\
**Post date:** [October 14, 2022, 8:27pm UTC](https://forum.inductiveautomation.com/t/how-to-catch-exceptions-from-database-calls-such-as-system-db-runprepupdate/65248/4 "2022-10-14T20:27:01Z")

</div>

Thank you!

I knew the trick about catching Java exceptions separately from one of your other replies, but I didn't know about this until your "Generic Python error handler" response.

`from java.lang import Throwable`

Great advice! It turns out in my example case at the top of the thread, referencing the .cause property of the Java exception provides the exact cause I was looking for:

```auto
> print e.cause

com.inductiveautomation.ignition.client.gateway_interface.GatewayException: SQL error for "INSERT INTO TestTable (val) VALUES (?)": Error converting data type nvarchar to float.

```
