# SQLServerException

**URL:** <https://forum.inductiveautomation.com/t/sqlserverexception/6276>\
**Category:** General Discussion\
**Created:** [April 19, 2013, 3:53pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276 "2013-04-19T15:53:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceidson](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@ceidson](https://forum.inductiveautomation.com/u/ceidson)\
**Post date:** [April 19, 2013, 3:53pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/1 "2013-04-19T15:53:21Z")

</div>

I get an exception infrequently when performing a write to a database table from a script called by a Gateway timer.

I have the write wrapped by try…except, and I write to a backup table if the first fails. In order to find the cause for the exception I need access to the GatewayException and\or SQLServerException from the traceback object. All I can seem to get is the Exception, which just tells me there was an error executing that query, not the cause.

I am testing using the following code that is ran by a button press. ProductionDemanded does not exist.

`import traceback
import system.db
try:
	a = system.db.runQuery("SELECT * FROM ProductionDemanded")
except:
	system.gui.messageBox("%s" % traceback.format_exc())`

How can I do this?

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [April 19, 2013, 9:45pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/2 "2013-04-19T21:45:57Z")

</div>

If you didn’t catch it, it would probably get logged. But I suppose you don’t want to kill your script over it. You could do this to log it to Log4J, which will include the entire stack and message:

```auto
from org.apache.log4j import LogManager
import java.lang.Exception

try:
	a = system.db.runQuery("SELECT * FROM ProductionDemanded")
except java.lang.Exception, err:
	LogManager.getLogger("MyScript").error("Error running query.", err)
```

Small side node: if you enclose code in “[code] [/ code]” it looks better.

Regards,

---

<div class="post-metadata">

**Author:** ![ceidson](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@ceidson](https://forum.inductiveautomation.com/u/ceidson)\
**Post date:** [April 22, 2013, 12:00pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/3 "2013-04-22T12:00:06Z")

</div>

No, I don’t want to kill the script, that is the purpose for the backup table. I will give the logging a shot. Thanks.

---

<div class="post-metadata">

**Author:** ![ceidson](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@ceidson](https://forum.inductiveautomation.com/u/ceidson)\
**Post date:** [April 22, 2013, 12:47pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/4 "2013-04-22T12:47:52Z")

</div>

Where is this log written?

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [April 22, 2013, 3:10pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/5 "2013-04-22T15:10:11Z")

</div>

From a gateway script, this would show up in the gateway console, under System\>Console in the gateway config. It also gets written to “wrapper.log” under “logs” in the install directory.

Regards,

---

<div class="post-metadata">

**Author:** ![ceidson](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@ceidson](https://forum.inductiveautomation.com/u/ceidson)\
**Post date:** [May 20, 2013, 4:39pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/6 "2013-05-20T16:39:29Z")

</div>

The above solution has been working great for SQL errors. Now I’d like to capture other errors. I was trying to capture the NameError for a test. I confirmed I am getting into the “except”, but then I get an TypeError returned by the getLogger statement. Is “MyScript” the problem?

```auto
from org.apache.log4j import LogManager
import java.lang.Exception
import exceptions
try
     ....
except java.lang.Exception, err:
     LogManager.getLogger("MyScript").error("Script Query Error.", err)
except NameError, err:
     LogManager.getLogger("MyScript").error("Name Error.", err)
except:
     #Something else
```

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [May 21, 2013, 9:10pm UTC](https://forum.inductiveautomation.com/t/sqlserverexception/6276/7 "2013-05-21T21:10:34Z")

</div>

Hard to say, what’s the details of your error now?
