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 ???
1 Answer
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)
3 Comments
Rod Daunoravicius
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:habnabit
@Rod: I would certainly hope not, as that would make the program impossible to debug.
nosklo
@Rod: A bare except should never be used unless the error is raised again, because it hides real errors.
sys.exit(0)does not produce an error message, I'll assume you're not talking about Python.