# Getting error using sys.exit() or exit()

**URL:** <https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176>\
**Category:** Ignition\
**Tags:** ignition81, scripting, python\
**Created:** [February 22, 2023, 5:38pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176 "2023-02-22T17:38:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Pawar](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Aman\_Pawar](https://forum.inductiveautomation.com/u/Aman_Pawar)\
**Post date:** [February 22, 2023, 5:38pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/1 "2023-02-22T17:38:32Z")

</div>

I've used sys.exit() function to get exit from script if certain conditions becomes true as given below:  
if (condition):  
import sys  
logged message  
sys.exit()

When this condition is satisfies, the message is getting logged in Ignition logs and also script is getting exit from the script by skipping later code in the script.  
But in the log getting message as E Dispatcher  
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 104, in valueChanged SystemExit

at org.python.core.PySystemState.exit(PySystemState.java:1523)

at org.python.core.PySystemState.exit(PySystemState.java:1530)

at jdk.internal.reflect.GeneratedMethodAccessor1299.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:188)

at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:206)

at org.python.core.PyObject. **call** (PyObject.java:480)

at org.python.core.PyObject. **call** (PyObject.java:484)

at org.python.core.PyMethod. **call** (PyMethod.java:126)

at org.python.pycode.\_pyx658427.valueChanged$1(:178)

at org.python.pycode.\_pyx658427.call\_function()

at org.python.core.PyTableCode.call(PyTableCode.java:171)

at org.python.core.PyBaseCode.call(PyBaseCode.java:308)

at org.python.core.PyFunction.function\_\_\_call\_\_(PyFunction.java:471)

at org.python.core.PyFunction. **call** (PyFunction.java:466)

at org.python.core.PyFunction. **call** (PyFunction.java:461)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:836)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:820)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:687)

at com.inductiveautomation.ignition.gateway.tags.scripting.TagScriptManagerImpl$FunctionInvokerImpl.run(TagScriptManagerImpl.java:530)

at com.inductiveautomation.ignition.gateway.tags.scripting.events.AbstractTagScript.invoke(AbstractTagScript.java:34)

at com.inductiveautomation.ignition.gateway.tags.scripting.TagScriptManagerImpl$Task.invoke(TagScriptManagerImpl.java:479)

at com.inductiveautomation.ignition.gateway.tags.scripting.TagScriptManagerImpl$TagScriptDispatcher.run(TagScriptManagerImpl.java:442)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:538)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 104, in valueChanged SystemExit

... 31 common frames omitted  
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  
I've also tried using exit() and observed same thing.  
This error is getting logged only once so I'm confused whether this method of using exit() or sys.exit() to exit code is valid in Ignition or not?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 22, 2023, 6:03pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/2 "2023-02-22T18:03:06Z")

</div>

Just use `return` to beak from the function.

```auto

def someFunction():
    if condition:
        #do something
        return
    elif someOtherCondition:
        #do something else
        return

    #fall through and do any last chance operation
    return

```

---

<div class="post-metadata">

**Author:** ![Aman\_Pawar](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Aman\_Pawar](https://forum.inductiveautomation.com/u/Aman_Pawar)\
**Post date:** [February 22, 2023, 6:10pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/3 "2023-02-22T18:10:09Z")

</div>

I'm not using any function, directly written script on tag on "value change" event.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 22, 2023, 6:13pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/4 "2023-02-22T18:13:24Z")

</div>

But you are. The valueChanged event is a function, Ignition just puts the function signature in for you because it needs to be specific.

```auto
def valueChanged(tag, tagPath, preiousValue, currentValue, initialChange, missedEvents):
    if initialChange:
        return

```

---

<div class="post-metadata">

**Author:** ![Aman\_Pawar](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Aman\_Pawar](https://forum.inductiveautomation.com/u/Aman_Pawar)\
**Post date:** [February 22, 2023, 6:25pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/5 "2023-02-22T18:25:16Z")

</div>

Ohh thats correct. But why getting error only first time after using sys.exit() or exit() ?  
When the condition triggers next time it does not log that error.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 22, 2023, 6:30pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/6 "2023-02-22T18:30:40Z")

</div>

You're probably shutting down the scripting 'engine' in the background that's responsible for running your code, and it's not restarted until the next time you make changes to the script.

Basically - never use `exit()` or `sys.exit()` in Ignition. If you want to exit the current _application_, use `system.util.exit()`.

---

<div class="post-metadata">

**Author:** ![Dawid\_Fulczynski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dawid_fulczynski/32/12308_2.png) [@Dawid\_Fulczynski](https://forum.inductiveautomation.com/u/Dawid_Fulczynski)\
**Post date:** [February 25, 2023, 11:04am UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/7 "2023-02-25T11:04:54Z")

</div>

Are there any better way to exit from e.g. actionPerformed button component script ?

eg.

`

```auto
import sys
if event.source.xSpecialProperty == 0:
	system.gui.messageBox(u"Special property not settled","Warn")
	  sys.exit()

#rest of code

```

is only proper way to move it to internal function and return ?

```auto
def action(event):
	if event.source.xSpecialProperty == 0:
		system.gui.messageBox(u"Special property not settled","Warn")
		return

	#rest of code
action(event)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 25, 2023, 1:17pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/8 "2023-02-25T13:17:14Z")

</div>

~~You don’t need the internal function.~~

 

Component scripts, really any scripts, are already functions.

~~Just returning in the scope of the `if` will work.~~

The `return` is saying I have finished my work return to the caller. No code in a function following a return will be executed. It ends the execution path.

In reality you are returning `none`, so any caller that is expecting a value back will receive a null value.

---

<div class="post-metadata">

**Author:** ![Dawid\_Fulczynski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dawid_fulczynski/32/12308_2.png) [@Dawid\_Fulczynski](https://forum.inductiveautomation.com/u/Dawid_Fulczynski)\
**Post date:** [February 25, 2023, 2:56pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/9 "2023-02-25T14:56:13Z")

</div>

Cannot save script with return without function.

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

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [February 25, 2023, 3:12pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/10 "2023-02-25T15:12:44Z")

</div>

~~In jython, the return statement requires a function:~~  
Disregard. I didn't see the overall context of the conversation

---

<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:** [February 25, 2023, 3:43pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/11 "2023-02-25T15:43:37Z")

</div>

> [@lrose](#):
>
> Component scripts, really any scripts, are already functions.

Not true in Vision, for actual events. Nor older project events. Custom methods and extension methods are functions.

> [@Dawid\_Fulczynski](#):
>
> Are there any better way to exit from e.g. actionPerformed button component script ?

No. Moving the logic to a project script library is the right answer. Which you should do anyways, to avoid legacy scoping issues in project events. And make your code more maintainable.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 25, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/getting-error-using-sys-exit-or-exit/70176/12 "2023-02-25T18:55:50Z")

</div>

I stand corrected.
