# Error Handling in tag event script

**URL:** <https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [October 14, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221 "2024-10-14T14:31:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [October 14, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221/1 "2024-10-14T14:31:51Z")

</div>

Hello,  
writing this script inside a tag event script generate a dispatcher log error as expected.

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value != previousValue.value :
        a = 2/0
        ...

```

If i write this function :

```python
def handler(tag, id_richiesta):

    logger_name = "test"
    log = system.util.getLogger(logger_name)
    log.trace("log 1")
    a = 2/0
    log.trace("log 2")

```

I call this function form the tag event script like so :

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value != previousValue.value :
	    id_richiesta = currentValue.value
	    RequestHandler.handler(tag, id_richiesta)

```

The result is that I find the trace message **"log 1"** in the logger, then the script stops working without giving any message.

If I place **"a = 2/0"** inside a try-catch block, the script behaves as expected.

I would like to understand what is happening, as my aim is to write an event handler function that will always return a message code to the PLC, so I would like to understand why, in this case, the script ends silently.

At the moment my idea is to use something like this, so i can notify plc and get detailed error in the ignition logger.

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	
	if currentValue.value != previousValue.value :
		id_richiesta = currentValue.value
		try : 
		    RequestHandler.handler(tag, id_richiesta)
		except : 
			## write to plc, not handled error
            ...
			raise

```

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 14, 2024, 2:52pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221/2 "2024-10-14T14:52:48Z")

</div>

I may not be understanding this correctly, but using the try-except block should solve your problem. Without it your script will encounter the unhandled exception following the 'Log 1' message and you will not be able to perform any additional actions.

If the `handler` function is executed without error, you can provide a success message after the function call in the `try` block (if necessary). Otherwise, I would write some logic in the except block to pass the message back to your PLC.

I am not sure if you are expecting any particular errors, but you can add several `except`s to handle each of these and set the code you wish to return to the PLC accordingly. The user manual has some good information on error handling [here](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/python-scripting/error-handling)

---

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [October 14, 2024, 3:01pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221/3 "2024-10-14T15:01:07Z")

</div>

Hello,  
Thank you for your answer.

To simplify my question, I would like to understand why an error gets notified by the ignition logger if it occurs inside a script written directly in the tag event script, but if the error occurs inside a project library function called by the tag event script, it won't be notified in the Ignition logger.

---

<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:** [October 14, 2024, 3:04pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221/4 "2024-10-14T15:04:38Z")

</div>

Test carefully. If you don't catch and log your own exceptions, you are at the mercy of the built-in logging, which generally changes the log level to DEBUG after the first error in a given script. DEBUG is hidden by default.

You **must** use `try:` - `except:` blocks if you want to **do** anything with an error.

---

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [October 14, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/error-handling-in-tag-event-script/97221/5 "2024-10-14T15:24:31Z")

</div>

I understand now, thank you.

As soon i set the script.dispatcher log level to debug, I'm able to see all the script error messages.

Is a bit confusing because the error messages are still marked with Error flag, but they only display if I turn on at least Debug mode but at least i know about this behavior.

Thank you for your time.
