Gubaer / josm-scripting-plugin

Task automation in the OpenStreetMap editor JOSM
https://gubaer.github.io/josm-scripting-plugin
GNU General Public License v3.0
26 stars 9 forks source link

How to stop script execution? #99

Closed jose1711 closed 1 year ago

jose1711 commented 1 year ago

While using jython backend - please what is the best way to stop execution of the script (without resorting to if.. then..)? Tried with System.exit(0) but that terminates the whole application (JOSM). Thank you.

Gubaer commented 1 year ago

You could define, raise and catch a custom error, say ScriptTerminateError.

# define the custom error
class ScriptTerminateError(Exception):
    pass

def some_other_procedure():
    # throw an an ScriptTerminateError
    raise ScriptTerminateError

def print_something():
    print("foo")
    some_other_procedure()

def script_main():
    print_something()

# wrap the invocation of the main method in a try .. except block.
# The code flow will end up here whenever you raise a   
# ScriptTerminateError somewhere in the script.
try:
    script_main()
except ScriptTerminateError:
    print("Script terminated with a termination error")