# Scripting - How to catch Raise from db function

**URL:** <https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [September 29, 2022, 12:16pm UTC](https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643 "2022-09-29T12:16:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [September 29, 2022, 12:16pm UTC](https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643/1 "2022-09-29T12:16:51Z")

</div>

I’m on 8.1.20

I have a PostgreSQL DB with functions that RAISE exception, I search the way to catch the RAISE message (highlighted)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/d/dd15afe5f62ee99b5c186fef740f3a57f47da0f5.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [September 29, 2022, 12:18pm UTC](https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643/2 "2022-09-29T12:18:33Z")

</div>

Database errors come through to ignition as java errors so you should use `java.lang.Exception` or preferably `java.lang.Throwable` to catch them like

```auto
import java.lang.Throwable
try:
    # some sql operation
except java.lang.Throwable, e:
    # catch exception

```

You may then want to examine `e.cause` to see the specific error. I use this to good effect.

---

<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:** [September 30, 2022, 11:41am UTC](https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643/3 "2022-09-30T11:41:07Z")

</div>

I havent used cause before, but I tend to use:

```auto
import traceback
system.perspective.print(traceback.format_exc())

```

Also, I use

```auto
from java.lang import Throwable
try:

except Throwable:
   import traceback....

```

It just avoids repeating things a bit

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [September 30, 2022, 1:09pm UTC](https://forum.inductiveautomation.com/t/scripting-how-to-catch-raise-from-db-function/64643/4 "2022-09-30T13:09:30Z")

</div>

The reason I like `e.cause` is its the exact error which allows me to easily catch for certain expected errors (my application is all forms so I have to expect Users to mess up)

so it allows me to do something like this

```auto
			if 'Duplicate entry' in str(e.cause):
				message = self.parseUniqueConstraintViolationErrorMessage(e, action)
			elif 'cannot be null' in str(e.cause):
				message = self.parseNullMessage(e, action)
			elif "Data too long" in str(e.cause):
				message = self.parseTooLongMessage(e, action)
			elif "Cannot delete or update a parent row" in str(e.cause) or "Cannot add or update a child row" in str(e.cause):
				message = self.parseFKConstraintMessage(e, action)
			elif "EmptyStringError" in str(e.cause):
				# Custom error we throw from the database via string. Error messages look like "EmptyStringError, rest of message here"
				message = self.parseEmptyStringError(e, action)
			else:

```

Then in my `else` is where I will log the entire stack trace as it's an unexpected database error.
