# Flow control to exit an event handler?

**URL:** <https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965>\
**Category:** Ignition\
**Created:** [December 29, 2015, 5:10pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965 "2015-12-29T17:10:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cezipper](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@cezipper](https://forum.inductiveautomation.com/u/cezipper)\
**Post date:** [December 29, 2015, 5:10pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/1 "2015-12-29T17:10:42Z")

</div>

Is there a statement that can exit an event handler in the logic so I can use an if statement to selectively not execute the rest of the handler?

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [December 29, 2015, 6:33pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/2 "2015-12-29T18:33:21Z")

</div>

Not that I’m aware of.

However it should be possible to accomplish whatever you need to achieve using “if”, “elif” and “else” statements and possibly the “break” statement to exit loops when a condition is met.

If there are multiple factors deciding whether script should execute or not you may have to create “flag” variables and use those to determine the flow of the script.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [December 29, 2015, 10:53pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/3 "2015-12-29T22:53:46Z")

</div>

Yes, you can use this:

`import sys
sys.exit(0)`

This will gracefully stop a Vision module event handler script

---

<div class="post-metadata">

**Author:** ![larryjones](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/larryjones/32/4102_2.png) [@larryjones](https://forum.inductiveautomation.com/u/larryjones)\
**Post date:** [October 22, 2019, 10:57pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/4 "2019-10-22T22:57:42Z")

</div>

I use the sys.exit() statement in event handlers without any issues but it throws an error when used in an Extension Function, at least it does in the onCellEdited Extension function of a Power Table. Is there a similar statement that works in an Extension Function?

---

<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 23, 2019, 12:56am UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/5 "2019-10-23T00:56:44Z")

</div>

Use `return`.

---

<div class="post-metadata">

**Author:** ![larryjones](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/larryjones/32/4102_2.png) [@larryjones](https://forum.inductiveautomation.com/u/larryjones)\
**Post date:** [October 24, 2019, 8:17pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/6 "2019-10-24T20:17:07Z")

</div>

That works well, thank you.

---

<div class="post-metadata">

**Author:** ![lrhyrne](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrhyrne/32/12097_2.png) [@lrhyrne](https://forum.inductiveautomation.com/u/lrhyrne)\
**Post date:** [December 3, 2019, 3:50pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/7 "2019-12-03T15:50:26Z")

</div>

Using Ignition 7.9.8 this approach does not work and still executes most of the script I am trying to avoid based on certain conditions…

```auto
import sys
if event.keyCode != event.VK_ENTER	:
	sys.exit(0)
	

bSuccess = False
username = ''
password = ''
userIP = ''

```

I am validating user login based on source IP and cannot have the full script being run every time.

---

<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:** [December 3, 2019, 3:53pm UTC](https://forum.inductiveautomation.com/t/flow-control-to-exit-an-event-handler/10965/8 "2019-12-03T15:53:11Z")

</div>

Move your script to a function in a project script module. Have the event script simply call that function, passing the event as a function argument. Use `return` in the function to exit early.
