0

I'm new to Python and I'm trying to use the exit function from sys, but somehow it's not working on my computer. This is my code:

from sys import exit

userResponse = input ("n or y?")
if not (userResponse =="n" or userResponse =="y") :
exit ("ERROR")
6
  • yes ("An exception has occurred, use %tb to see the full traceback. SystemExit: ERROR") Commented Oct 19, 2017 at 14:21
  • that code is not even valid, please fix it. Commented Oct 19, 2017 at 14:27
  • Which python version and how do you run the code ? Commented Oct 19, 2017 at 14:35
  • python 3.6 and spyder 3.1.4 editor Commented Oct 19, 2017 at 14:38
  • I think this question is fine: I agree it's a little unintuitive that the "system exit" function may spit out an exception, implying (to someone who doesn't know how it works) that something went wrong, even when the exit was perfectly fine Commented Oct 19, 2017 at 14:38

4 Answers 4

3

I was running into issues while trying to exit a script without raising an exception. What worked best for my case was os._exit(exitcode).

Sign up to request clarification or add additional context in comments.

Comments

1

Exit raises a SystemExit exception; that's how it terminates the program. That your environment displays that exception after terminating is not a bug, it's just showing you the cause. I agree with you (as does, e.g., this PEP), that the default behaviour should be to not print out a traceback message for this exception, and for KeyboardException

But in the end, the execution halted, as it was designed to do.

See the docs for details: https://docs.python.org/2/library/sys.html#sys.exit


A few answers here initially indicated that you cannot pass a string to exit. That is false: from the docs (emphasis mine)

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise.... If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.


The builtin exit function does the same thing. In general, it is not great to shadow the builtin function by importing from a module, though in this case it doesn't matter. Recommended style would be

import sys
sys.exit("My exception")

If this seems unintuitive to you, you can bypass the exception handling by using os._exit. Don't do these without cause

Comments

1

You have to indent correctly:

if not (userResponse =="n" or userResponse =="y") :
    exit('Error')

You could also raise an exception to get an even more descriptive error message:

userResponse = input ("n or y?")
if not (userResponse =="n" or userResponse =="y") :
    raise ValueError('Does not work')

11 Comments

Extended my answer.
I tried writing 'exit("ERROR")' in my python terminal and it worked fine. When does it need to be an integer? (I also think leaving it blank would work)
this a link to a picture of the error massage i still get e.top4top.net/p_657e40e81.png
@jooj add that information to your question (not as a picture) and you'll get better answers. @ mrCarnivore from the docs "sys.exit("some error message") is a quick way to exit a program when an error occurs"
@mrCarnivore : From the builtin help : exit(...) exit([status]) Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure).
|
0

Following up on @MrCarnivore...

from sys import exit

userResponse = raw_input("n or y?")
if not (userResponse =="n" or userResponse =="y"):
     print('Error Message Here')
     exit(1)

4 Comments

i get this error massage :(Error An exception has occurred, use %tb to see the full traceback. SystemExit: 1 /Users/juhina/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
When does exit require an integer? On my interactive console, 2.7, it works fine with "exit('MESSAGE')"
@cer : """exit(...) exit([status]) Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure).""" - IOW passing a message is perfectly legal. Also if you want to print errors, print them on sys.stderr, sys.stdout is for normal program outputs.
@jooj you are confused because you are running this from ipython, which is catching the SystemExit exception for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.