views:

2149

answers:

1

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 ???

+5  A: 

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)
Gonzalo Quero
+1 for deciphering the question =]
mdec
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 Daunoravicius
@Rod: I would certainly hope not, as that would make the program impossible to debug.
Aaron Gallagher
@Rod: A bare except should never be used unless the error is raised again, because it hides real errors.
nosklo