# Jython Script Exit

**URL:** <https://forum.inductiveautomation.com/t/jython-script-exit/27905>\
**Category:** Ignition\
**Created:** [August 30, 2019, 1:22pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905 "2019-08-30T13:22:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 30, 2019, 1:22pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/1 "2019-08-30T13:22:56Z")

</div>

I have some scripts that utilize several if statements. If the script enters a certain one of the if statements, I want the script to stop execution and exit. I’ve been doing this with

```auto
from sys import exit
exit()

```

Do I need to do this, or is there a better, more appropriate way to do this?

---

<div class="post-metadata">

**Author:** ![boyergary](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/boyergary/32/10316_2.png) [@boyergary](https://forum.inductiveautomation.com/u/boyergary)\
**Post date:** [August 30, 2019, 1:26pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/2 "2019-08-30T13:26:53Z")

</div>

You can simply call “return” to exit from the script at any point.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 30, 2019, 2:01pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/3 "2019-08-30T14:01:48Z")

</div>

I think this will work, but only if I define the entire script as a function. Just in case someone else wonders about this.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 18, 2022, 6:52pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/4 "2022-10-18T18:52:21Z")

</div>

What did you end up doing?

Is exit() okay?  
Do I have to import that library?

I have people submitting, but I want them not to be able to if some values are null.  
I don't import the library and use exit(), seems to work, but I don't know if that means it is the way to perform the script.

---

<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:** [October 18, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/5 "2022-10-18T19:16:54Z")

</div>

Validate that all fields that you need are not null and then execute, otherwise just fall through. Though you should give the user some indication of why there submission was not accepted.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 18, 2022, 8:06pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/6 "2022-10-18T20:06:18Z")

</div>

Yah, I should make some exception logic that sets text.  
If I could just remember where you showed me that.  
I remember seeing it, and even implementing it for a time once.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 30, 2023, 7:27pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/7 "2023-08-30T19:27:34Z")

</div>

```auto
if range(dataOut.getRowCount()) == []:
	print 'yes'
	exit()
print 'did not stop'

```

Right now, this code snippet is performing as I hoped in the script console.  
I am still not clear on if exit() is good to use though.  
Return won't work, says 'return' outside function.

---

<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:** [August 30, 2023, 7:32pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/8 "2023-08-30T19:32:01Z")

</div>

I would strongly recommend that you not use exit. This isn't a complete snip-it of code, where ever you are really going to place it, will be in a function of some type, so return will work.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 30, 2023, 8:13pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/9 "2023-08-30T20:13:36Z")

</div>

Thanks

Is there not another way to end a script such that it will work in the script console and where the code will be placed?

I think I only struggle with this because return is problematic for me in the script console.

---

<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:** [August 30, 2023, 8:17pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/10 "2023-08-30T20:17:02Z")

</div>

Just put it in a dummy function, if you really must use return.

Other wise, either you get two print statements or you only the last one.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 30, 2023, 8:29pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/11 "2023-08-30T20:29:16Z")

</div>

You can also express almost any logic without resorting to early return/exit with a little refactoring.

Your original example is simple but still demonstrates the point:

```auto
if range(dataOut.getRowCount()) == []:
	print 'yes'
else:
	print 'did not stop'

```

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 30, 2023, 8:44pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/12 "2023-08-30T20:44:19Z")

</div>

Thanks

I read that returning early is better than not.

Guess I have to pick between:  
Returning early where I place the code while using exit in the script console  
Returning early where I place the code while using a dummy function with return early in the script console  
Or placing my code inside the if statement

Also, I apologize for writing:  
`if range(dataOut.getRowCount()) == []:`  
I was learning how to find out if my query came back empty.  
`if dataOut.getRowCount() ==0:`

---

<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:** [August 30, 2023, 9:20pm UTC](https://forum.inductiveautomation.com/t/jython-script-exit/27905/13 "2023-08-30T21:20:17Z")

</div>

> [@zacharyw.larson](#):
>
> if dataOut.getRowCount() ==0:

This:

```python
if dataOut.rowCount == 0:

```

Is more performant.

You can also use the fact that 0 is considered falsey and just write

```python
if dataOut.rowCount:

```
