Capturing MySQL specific exception

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()))

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

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:

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

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