Repl process died unexpectedly after exit()

This is intentional. In my code, I’m using if statements instead of while True loops which means I can’t use a break statement.

start = input("Would you like to begin the quiz? (y/n): ")

if start == 'y':
  print("Okay!\n")
  time.sleep(1)
  def load_Animation():
    animation = "|/-\\"

    for i in range(20):
        time.sleep(0.1)
        sys.stdout.write("\r" + animation[i % len(animation)])
        sys.stdout.flush()

  print("Loading ....")
  load_Animation()
  print(" ")
  os.system('cls' if os.name == 'nt' else 'clear')
  pass
elif start == 'n':
  print("Okay!")
  exit()
else:
  print("Invalid input!")
  exit()

I use the exit() to quit the repl but is there a way to make it print anything other than repl process died unexpectedly?

You can use break statements inside if loops as long as the if loop themselves are in a for or while loop.

Try it out!

3 Likes

First of all welcome to the community @EvanSprouse1 but yes it will print out that after but like @QuantumCodes said if you have a while loop you can do break() and be able to do it that way. Also for other times, I would suggest using #code-help:python so others can help you as well.

Hm, I’m just spit balling an idea here. Put all of the code inside of one for loop, but make the for loop only run once, so for i in range(1):. Then it will allow you to be able to use break syntax instead of exit() because it’s still technically considered a loop.

2 Likes

Adding on to this, you can (I think) catch the exit call (wrapped in a try-except block), that might only work for sys.exit() though. Regardless, while this is possible, it shouldn’t be done in practice, as you can’t catch the error/exception raised by exit, so you have to use a blank try-except, which is never a good idea. It is an option though.

Edit, second idea:

  1. Have 2 py files, main.py and another one (say code.py)
  2. Move all your code to your second file (code.py for this example), and place it under a function called “main”
  3. Change your main.py file to the following (Changing code to the name of your other file (if you use my example name, then leave the name unchanged)):
from code import main
main()
  1. Instead of exits in your code, use return instead
  2. Run your repl, it should no longer exit unexpectedly

I wonder if you even need to break/exit in this situation. The program will function basically equivalently if you remove the exit() calls, because there isn’t anything more to run after both of them

1 Like

Now i may be wrong but since the code sys.exit() runs successfully isn’t there nothing to catch?

I will check again, but IIRC it raises an error under the hood.

From Stack Overflow:

The docs say that calling sys.exit() raises a SystemExit exception which can be caught in outer levels.

From the docs:

Raise a SystemExit exception, signaling an intention to exit the interpreter.

I think it raises a SystemExit

1 Like

if it raises anything, can’t we use the raise keyword in a way to make our own raised message to overwriting the original?

Yes, but why would you need to do that?

class ProgramStopped(Exception):
    pass

try:
    sys.exit()
except:
    raise ProgramStopped("hi")
Traceback (most recent call last):
SystemExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
__main__.ProgramStopped: hi
1 Like

what about raise ProgramStopped("Hi") from None

That would just hide the information about SystemExit, right? I’m still not sure how that would be useful (you could just throw whatever error you wanted rather than catching a call to sys.exit)