# Catching errors in Python

**URL:** <https://forum.inductiveautomation.com/t/catching-errors-in-python/748>\
**Category:** Archived Topics\
**Created:** [July 29, 2008, 9:05pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748 "2008-07-29T21:05:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [July 29, 2008, 9:05pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/1 "2008-07-29T21:05:25Z")

</div>

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 code`try:
    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?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [July 29, 2008, 10:40pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/2 "2008-07-29T22:40:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 4, 2010, 6:32pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/3 "2010-03-04T18:32:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [March 4, 2010, 8:29pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/4 "2010-03-04T20:29:07Z")

</div>

That’s great ![:thumb_left:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e26afb081ddde6df016df6ee49159cf56aaca688.gif ":thumb\_left:")

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

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 4, 2010, 9:53pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/5 "2010-03-04T21:53:17Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![wakaguaka](https://avatars.discourse-cdn.com/v4/letter/w/ea666f/32.png) [@wakaguaka](https://forum.inductiveautomation.com/u/wakaguaka)\
**Post date:** [July 5, 2019, 10:55pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/6 "2019-07-05T22:55:57Z")

</div>

> [@Carl.Gould](#):
>
> errinfo.cause.message

Hi,  
I am trying in my except block:

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

---

<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:** [July 5, 2019, 11:50pm UTC](https://forum.inductiveautomation.com/t/catching-errors-in-python/748/7 "2019-07-05T23:50:04Z")

</div>

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

Try code something like this:

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

```
