# Try Catch Block is not catching

**URL:** <https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547>\
**Category:** Ignition\
**Created:** [November 11, 2013, 3:20am UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547 "2013-11-11T03:20:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [November 11, 2013, 3:20am UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/1 "2013-11-11T03:20:55Z")

</div>

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]
```

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [November 11, 2013, 3:20pm UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/2 "2013-11-11T15:20:23Z")

</div>

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:`

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [November 11, 2013, 4:03pm UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/3 "2013-11-11T16:03:08Z")

</div>

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:

```auto
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))
```

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [November 11, 2013, 4:07pm UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/4 "2013-11-11T16:07:31Z")

</div>

Good catch, Nick!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [November 11, 2013, 4:08pm UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/5 "2013-11-11T16:08:51Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [November 12, 2013, 2:23am UTC](https://forum.inductiveautomation.com/t/try-catch-block-is-not-catching/7547/6 "2013-11-12T02:23:12Z")

</div>

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

```auto
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.
