# Try Except FOREIGN KEY error

**URL:** <https://forum.inductiveautomation.com/t/try-except-foreign-key-error/51138>\
**Category:** Ignition\
**Tags:** scripting, ignition80\
**Created:** [September 20, 2021, 11:28am UTC](https://forum.inductiveautomation.com/t/try-except-foreign-key-error/51138 "2021-09-20T11:28:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![indenkleefj](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/indenkleefj/32/10961_2.png) [@indenkleefj](https://forum.inductiveautomation.com/u/indenkleefj)\
**Post date:** [September 20, 2021, 11:28am UTC](https://forum.inductiveautomation.com/t/try-except-foreign-key-error/51138/1 "2021-09-20T11:28:06Z")

</div>

I have some code that’s wrapped in a try-except block.  
In this try-except block, there is a query that fails and gives a FOREIGN KEY error.  
When this error happens for some reason the code fails and doesn’t go to the except part.  
This only happens when I use `except Exception, e:` instead of `except:`.  
When using `except:` the code gets in the exception part.  
So for now I’m using `except:` but I would love to know why `except Exception, e:` doesn’t work.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [September 20, 2021, 11:45am UTC](https://forum.inductiveautomation.com/t/try-except-foreign-key-error/51138/2 "2021-09-20T11:45:32Z")

</div>

You are probably getting a java.lang.Expection instead of a python Expection raised by the system.db function.  
try:  
`from java.lang import Exception`

or maybe better to avoid it clashing with python:

```auto
from java.lang import Exception as JavaExpection
	try:
		system.db.runQuery('query')
	except (JavaExpection, Exception), e:		
		return str(e)

```

---

<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:** [September 20, 2021, 12:11pm UTC](https://forum.inductiveautomation.com/t/try-except-foreign-key-error/51138/3 "2021-09-20T12:11:25Z")

</div>

A post I wrote a while back about properly using try/except with Database restraints - [Insert into database only if the entry doesn't exist - #2 by bkarabinchak.psi](https://forum.inductiveautomation.com/t/insert-into-database-only-if-the-entry-doesnt-exist/35122/2)
