0

I want to stop a Python script on seeing an error message.
I dont want any error message on shell like exit().
How to do it ???

3
  • Could you please provide some code or at least a better description of what you are trying to achieve? Commented Jan 27, 2009 at 12:27
  • Since sys.exit(0) does not produce an error message, I'll assume you're not talking about Python. Commented Jan 27, 2009 at 12:28
  • @S. Lott I think he means "I don't want any error message on shell, but a clean exit as sys.exit() does". Commented Jan 27, 2009 at 12:49

1 Answer 1

9

When you send CTRL+C to a Python script, it raises the KeyboardInterrupt exception, so you can do something like

try:
    ... Work goes here ...
except KeyboardInterrupt:
    sys.exit(0)
Sign up to request clarification or add additional context in comments.

3 Comments

I think he actually meant to ask how to suppress the error message/traceback at ANY error, so the 3rd line would be except: instead of except KeyboardInterrupt:
@Rod: I would certainly hope not, as that would make the program impossible to debug.
@Rod: A bare except should never be used unless the error is raised again, because it hides real errors.

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.