1

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 sys.exit():

def reset():
    """Reset the complete logic. Equivalent to unplugging and plugging again."""

    sys.exit()

And try catching SystemExit.

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 loop.

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?

  • Welcome - this is not technically a Pi specific problem - more a general programming on so it may get moved or closed despite my answer below :) – Andyroo Jun 17 at 12:52
  • Hi @Andyroo , thanks for your comment. If I run the code in my MacBook and raise the system.exit() error, the code keeps running in the loop. So it seems that the behavior is different when the code is running in the raspberry pi. Does that make sense? – J0ANMM Jun 17 at 13:10
  • Sorry - I’ve not really done that much Python on the Mac (It’s still V2 if I remember on Mojave - the latest I’m at) and my box is currently off my desk due to my other hobby materials involve paint and water! Maybe worth asking on the software development board. – Andyroo Jun 17 at 15:55
1

sys.exit() or technically sys.exit(0) is designed to end the Python executable and therefore your program as well.

This is documented here for Python 3.7

Key thing to note is

so cleanup actions specified by finally clauses of try statements are honored

Hence the final message from your program in the except clause.

Using your own bespoke exception is more controllable than a system level one unless you want to pass the ‘error’ back up the program stack e.g. to a calling shell script.

| improve this answer | |
  • I see... I don't get why the loop keeps running in my MacBook then... Anyway, any idea why the customized error is not caught? – J0ANMM Jun 17 at 13:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.