MySQL Exception Handling on Duplicate Entry

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?


What’s your current code?

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

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

Hey guys thanks for chiming in.

My code is simple:

 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.


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.

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!