I often like to exit early from Jython functions I write by returning. This helps keep nested if conditions to a minimum.
For instance, I’d check if data in arguments are valid and if they are not then return right away.
But I couldn’t do this when writing event handlers because event handlers are not functions. I could wrap the event handlers in functions but that’s extra work and an extra level of indentation.
But with Ignition release 7.1.2, there is now a way to exit early from event handler code. Calling sys.exit() kills the script it is in. Code past sys.exit() is not run.
I also use exiting early for testing code. I put code I want to test at the top of a function or event handler, and then exit early with return or sys.exit() so all the other code that I’m not testing isn’t run.
What do you mean? As described above, sys.exit() kills the interpreter running that one event script, not the whole client. To kill the whole client, try this:
I mean I have a script with a confirmation dialog box. An action is taken when either “Yes” or “No” are selected. I want the script to stop running when the “Cancel” option is selected as well. When I use sys.exit() I get an error saying the name sys is not defined.
This all works fine until I try to use in an system.util.invokeAsynchronous thread.
I am not trying to kill the client, just the thread. I have tried import sys, exit, quit sys.exit, and sys.quit. All throw an error.