# Script failing despite being within a try/except

**URL:** <https://forum.inductiveautomation.com/t/script-failing-despite-being-within-a-try-except/30757>\
**Category:** Ignition\
**Created:** [December 17, 2019, 8:16am UTC](https://forum.inductiveautomation.com/t/script-failing-despite-being-within-a-try-except/30757 "2019-12-17T08:16:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 17, 2019, 8:16am UTC](https://forum.inductiveautomation.com/t/script-failing-despite-being-within-a-try-except/30757/1 "2019-12-17T08:16:13Z")

</div>

I have this code that is failing at the SQL query and does **not execute the except code**. Basically I want to write to some tags if this script fails. Sometimes the end date is null and so this SQL INSERT will fail, as in this example.

```auto
try:
	logged_on = now
	ret = system.db.runPrepUpdate('INSERT INTO datSeqLogs (seq_id, started_on, stopped_on, completed, logged_datetime) VALUES (?,?,?,?,?)', [seq_id, startDate, stopDate, completed, logged_on], SQL_CONNECTION_NAME)
	seq_logs_id = system.db.runPrepQuery('SELECT id FROM datSeqLogs WHERE seq_id = ? AND logged_datetime = ? AND started_on = ? AND stopped_on = ? AND completed = ?', [seq_id, logged_on, startDate, stopDate, completed], SQL_CONNECTION_NAME)[0]['"d"] #ignore the extra ' here, used to fix forum colour formatting
except Exception, e:
	error = '<HTML>Failed to write sequence log into datSeqLogs: INSERT INTO datSeqLogs (seq_id, started_on, stopped_on, completed, logged_datetime) VALUES (%s, %s, %s, %s, %s)<P>' % (seq_id, startDate, stopDate, completed, now)
	error += 'or failed to run: SELECT id FROM datSeqLogs WHERE seq_id = %s AND logged_datetime = %s AND started_on = %s AND stopped_on = %s AND completed = %s' % (seq_id, logged_on, startDate, stopDate, completed)
	system.tag.write(sequenceTagPath + '/Log/Common/Log Failed', 1)
	system.tag.write(sequenceTagPath + '/Log/Common/Log Script Running', 0)
	system.tag.write(sequenceTagPath + '/Log/Common/Last Log Error', error)
	LOGGER.error(e)
	LOGGER.error("Sequence logging failed for sequence %s" % sequenceTagPath)
	return

```

Error:

```auto
Error executing script.
Traceback (most recent call last):
  File "<tagevent:valueChanged>", line 4, in valueChanged
  File "<module:shared.sequences.logging>", line 123, in logSeqData
  File "<module:shared.sequences.logging>", line 123, in logSeqData
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258)
	at sun.reflect.GeneratedMethodAccessor132.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO datSeqLogs (seq_id, started_on, stopped_on, completed, logged_datetime) VALUES (?,?,?,?,?), [0300, Tue Nov 19 07:07:22 ACDT 2019, , 1, Tue Dec 17 18:39:59 ACDT 2019], SQLServer, , false, false)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2019, 11:17am UTC](https://forum.inductiveautomation.com/t/script-failing-despite-being-within-a-try-except/30757/2 "2019-12-17T11:17:32Z")

</div>

Python's Exception class is not the same thing as java.lang.Exception. You'll need a separate except clause for it. Something like this:

> [@Catch IOException?](http://forum.inductiveautomation.com/t/catch-ioexception/18612/3):
>
> Consider using two except blocks, as jython’s standard Exception won’t catch pure java exceptions. Like so: import java.lang.Exception try: # something something something except Exception, e: # jython exception except java.lang.Exception, e: # java exception

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 17, 2019, 12:31pm UTC](https://forum.inductiveautomation.com/t/script-failing-despite-being-within-a-try-except/30757/3 "2019-12-17T12:31:59Z")

</div>

Thanks Phil, I’m sure I knew this at one stage!
