# Named Queries error trapping

**URL:** <https://forum.inductiveautomation.com/t/named-queries-error-trapping/16827>\
**Category:** General Discussion\
**Tags:** namedqueries\
**Created:** [January 31, 2018, 6:52pm UTC](https://forum.inductiveautomation.com/t/named-queries-error-trapping/16827 "2018-01-31T18:52:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [January 31, 2018, 6:52pm UTC](https://forum.inductiveautomation.com/t/named-queries-error-trapping/16827/1 "2018-01-31T18:52:18Z")

</div>

I’ve started using Named Queries and I LOVE them.  
Is there a way to trap the errors, for example:  
 Unique Key violated  
 Parent Key not found  
 Insufficient Privileges  
 etc.  
I’m using a try/except block like this:

try:  
 runNamedQuery( ‘myQueryName’, {‘partNumber’:pn,‘lotNumber’:lot} )  
except:  
 logger.error(‘myQueryName failed…’)

but I would like to capture a meaningful message vs. “something went wrong.”

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [January 31, 2018, 7:14pm UTC](https://forum.inductiveautomation.com/t/named-queries-error-trapping/16827/2 "2018-01-31T19:14:56Z")

</div>

Does this work for you?:

> `import sys try: results = system.db.runNamedQuery("queryName") except:Preformatted text logger.error('queryName failed' + sys.exc_info()[1]) `

sys.exc\_info()[0] contains the type of the error and [1] contains the message afaik.

edit: Forgot the import statement at the top. Couldn't format the indentation either...

---

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [January 31, 2018, 9:28pm UTC](https://forum.inductiveautomation.com/t/named-queries-error-trapping/16827/3 "2018-01-31T21:28:24Z")

</div>

Yes, that’s exactly what I needed.  
Had to add “import sys” at the beginning of my script and it works like a charm.  
Thank You
