Try Catch Block is not catching

I have the following script where im trying to insert into a MSSQL db, i have some constraints that prevent inserts with the same values, I was using the try catch block to catch the DB error where the insert fails.

For some reason the insert fails and I get the Java error popup box. Im sure its something im not doing right, any suggestions? Thanks

[code]try:
# Insert new roll into DB
rollID = system.db.runUpdateQuery(“INSERT INTO upe_rolls (lotNumID, recipeID, rollNum) VALUES (%d, %d, %d)” % (lotNumID, recipeID, rollNum), getKey=1)

	# Save the RollID into a tag for setting the roll length when finished
	system.tag.write("Extrusion/Lot Tracking/Roll_ID", rollID)
	print 'New Roll with ID %d Was Inserted' % rollID
except Exception, e:
	system.gui.messageBox(e)[/code]

Two things jump out at me.

First, is your code here formatted exactly the way it is in your script? The catch block should be at the same indent level as your try block or Python will silently fail.

Also, I’m not familiar with except Exception, e: Is that legal Python? I’ve always seen it as except Exception as e:

system.db.runUpdateQuery throws a Java exception (java.lang.Exception), not a Python exception.

Since your code tries to catch a Python exception and not a Java exception it doesn’t work.

Doing this works:

import java
try:
      # Insert new roll into DB
      rollID = system.db.runUpdateQuery("INSERT INTO upe_rolls (lotNumID, recipeID, rollNum) VALUES (%d, %d, %d)" % (lotNumID, recipeID, rollNum), getKey=1)
   
      # Save the RollID into a tag for setting the roll length when finished
      system.tag.write("Extrusion/Lot Tracking/Roll_ID", rollID)
      print 'New Roll with ID %d Was Inserted' % rollID
except java.lang.Exception, e:
      system.gui.messageBox(str(e))
1 Like

Good catch, Nick!

If you want to catch any exception and also get information about what exception was raised, you could do it like this:

import sys
try:
      # Insert new roll into DB
      rollID = system.db.runUpdateQuery("INSERT INTO upe_rolls (lotNumID, recipeID, rollNum) VALUES (%d, %d, %d)" % (lotNumID, recipeID, rollNum), getKey=1)
   
      # Save the RollID into a tag for setting the roll length when finished
      system.tag.write("Extrusion/Lot Tracking/Roll_ID", rollID)
      print 'New Roll with ID %d Was Inserted' % rollID
except:
      system.gui.messageBox("%s, %s"% (sys.exc_info()[0],sys.exc_info()[1]))

Cheers,
Nick

1 Like

[quote=“nmudge”]If you want to catch any exception and also get information about what exception was raised, you could do it like this:

import sys
try:
      # Insert new roll into DB
      rollID = system.db.runUpdateQuery("INSERT INTO upe_rolls (lotNumID, recipeID, rollNum) VALUES (%d, %d, %d)" % (lotNumID, recipeID, rollNum), getKey=1)
   
      # Save the RollID into a tag for setting the roll length when finished
      system.tag.write("Extrusion/Lot Tracking/Roll_ID", rollID)
      print 'New Roll with ID %d Was Inserted' % rollID
except:
      system.gui.messageBox("%s, %s"% (sys.exc_info()[0],sys.exc_info()[1]))

Cheers,
Nick[/quote]

Thanks Nick,

I appreciate your help, that’s exactly what i wanted.