I am trying to implement a RESET button, so when pressing the button, the code restarts.
However, I am getting some strange behavior.
First I tried raising a custom Reset Exception:
class Reset( Exception ): """Used to get out to the starting point through a specific error than can be caught via 'raise Reset' and 'except Reset'""" pass
so then I can call the following function anywhere in my code and catch the Exception at the outer loop to restart.
def reset(): """Reset the complete logic. Equivalent to unplugging and plugging again.""" raise Reset('Reset button pressed')
Unfortunately, for some reason after the error is raised, it is not caught and the code keeps running where it was before pressing the button.
I then decided to change my
reset function and use
def reset(): """Reset the complete logic. Equivalent to unplugging and plugging again.""" sys.exit()
And try catching
This way, the error is caught, but the code does stop for some reason at the end of the exception and does not keep running in the
while True: print('New iteration of main loop...') print() try: # do whatever (this includes additional loops) # except Reset: except SystemExit as e: #if error caught, go on with the next iteration of the loop print('Reset exception caught! Waiting 5 seconds...') time.sleep(5) # pass
The sentence "Reset exception caught! Waiting 5 seconds..." is printed, but after that, the code stops, and a message asking for a manual restart appears.
Any idea why is it not working?