# Capturing MySQL specific exception

**URL:** <https://forum.inductiveautomation.com/t/capturing-mysql-specific-exception/18165>\
**Category:** Ignition\
**Created:** [May 25, 2018, 2:50am UTC](https://forum.inductiveautomation.com/t/capturing-mysql-specific-exception/18165 "2018-05-25T02:50:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ella\_ramos](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ella_ramos/32/3455_2.png) [@ella\_ramos](https://forum.inductiveautomation.com/u/ella_ramos)\
**Post date:** [May 25, 2018, 2:50am UTC](https://forum.inductiveautomation.com/t/capturing-mysql-specific-exception/18165/1 "2018-05-25T02:50:00Z")

</div>

Hello everyone,

How can I capture MySQL specific exception?

This is my code and I want to be able to capture duplicate exception and handle that and then all the other exception. But this gives me NameErrror saying MySQLIntegrityConstraintViolationException does not exist.

> try:  
> system.db.runPrepUpdate(insert, [tooltypeid, type,name])  
> event.source.parent.getComponent('notLbl').text = name + " is added to the Database successfully!"  
> event.source.parent.getComponent('notLbl').visible = True
> 
> ```
> event.source.parent.getComponent('notLbl').foreground = "#000"
> event.source.parent.getComponent('typeTxt').text = ""
> event.source.parent.getComponent('nameTxt').text = ""
> 
> ```
> 
> except MySQLIntegrityConstraintViolationException:  
> system.gui.messageBox(name + " already exists!")  
> except:  
> print traceback.format\_exc()  
> system.gui.messageBox(str(traceback.format\_exc()))

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [May 25, 2018, 6:57am UTC](https://forum.inductiveautomation.com/t/capturing-mysql-specific-exception/18165/2 "2018-05-25T06:57:06Z")

</div>

So, I haven’t tested yet, but I believe that exception only exists in the gateway scope. Even though you are running the script from a client, it gets executed on the GW. If the script originates from the gateway, you could do the following

```auto
from com.mysql.jdbc.exceptions.jdbc4 import MySQLIntegrityConstraintViolationException

try:
    #run query
except MySQLIntegrityConstraintViolationException,e:
    do something with e

```

You can not import that class on the client, however.

That exception comes from com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException. Because that is an exception from Java, you can catch general java exceptions like the following:

```auto
from java.lang import Exception as JavaException
try:
    do something
except JavaException, je:
    print "java exception"
except Exception,e:
    print "jython exception"

```

---

<div class="post-metadata">

**Author:** ![ella\_ramos](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ella_ramos/32/3455_2.png) [@ella\_ramos](https://forum.inductiveautomation.com/u/ella_ramos)\
**Post date:** [May 28, 2018, 1:36am UTC](https://forum.inductiveautomation.com/t/capturing-mysql-specific-exception/18165/3 "2018-05-28T01:36:11Z")

</div>

thanks, I think I will just go with the latter example. I will try that.
