# MySQL Exception Handling on Duplicate Entry

**URL:** <https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361>\
**Category:** Ignition\
**Created:** [December 19, 2017, 3:07pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361 "2017-12-19T15:07:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctonge](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@ctonge](https://forum.inductiveautomation.com/u/ctonge)\
**Post date:** [December 19, 2017, 3:07pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/1 "2017-12-19T15:07:39Z")

</div>

Hi, I’m not much of a ninja on exception handling in general. In this case I’m trying to gracefully handle a duplicate constraint exception. Currently I’m just trying to capture it with a generic Exception handler, but it still raises the error, and ignores my code to pop-up a message box instead.

Here is the exception with query details ommitted:

_Traceback (most recent call last):_  
_File “event:actionPerformed”, line 20, in_   
\_ File “event:actionPerformed”, line 20, in \_  
_java.lang.Exception: java.lang.Exception: Error executing …_

\_ caused by Exception: Error executing system.db.runPrepUpdate…\_

\_ caused by GatewayException: SQL error for …\_  
_Duplicate entry ‘1234-5-7’ for key ‘unq\_partno\_locationid’_  
\_ caused by MySQLIntegrityConstraintViolationException: Duplicate entry ‘1234-5-7’ for key ‘unq\_partno\_locationid’\_

_Ignition v7.9.4 (b2017082911)_  
_Java: Oracle Corporation 1.8.0\_151_

So does it require me to be more specific in the Try/Except to handle the MySQLIntegrityConstraint exception?

Thanks!

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [December 20, 2017, 7:04am UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/2 "2017-12-20T07:04:23Z")

</div>

What’s your current code?

---

<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:** [December 20, 2017, 2:48pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/3 "2017-12-20T14:48:22Z")

</div>

In jython, except clauses can specify java exception types in addition to python exceptions. Use something like this:

```auto
import java.lang.Exception
try:
  # some code that can throw a non-python exception
except java.lang.Exception, e1:
  # Handle the java exception
except:
  # Handle any other exception

```

---

<div class="post-metadata">

**Author:** ![ctonge](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@ctonge](https://forum.inductiveautomation.com/u/ctonge)\
**Post date:** [December 20, 2017, 3:35pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/4 "2017-12-20T15:35:54Z")

</div>

Hey guys thanks for chiming in.

My code is simple:

```auto
 try:
     system.db.runPrepUpdate(query,[params])
 except Exception, e:
     system.gui.messageBox('blah blah')

```

The try never catches the exception and runs the message box code. It always raises the exception in the ignition error popup. Regardless of what type of exception I look for.

Thoughts?

---

<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:** [December 20, 2017, 6:29pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/5 "2017-12-20T18:29:42Z")

</div>

Follow my instructions. Python defines it’s own Exception datatype, and only catches that and its subclasses by default. To catch non-python exceptions, you must explicit catch java.lang.Exception.

---

<div class="post-metadata">

**Author:** ![ctonge](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@ctonge](https://forum.inductiveautomation.com/u/ctonge)\
**Post date:** [December 21, 2017, 7:26pm UTC](https://forum.inductiveautomation.com/t/mysql-exception-handling-on-duplicate-entry/16361/6 "2017-12-21T19:26:10Z")

</div>

Sorry, I breezed right past your post and clearly didn’t pick up on the pertinent details. That is working and now I get the difference with the types of exceptions. Cheers!
