user46646
user46646

Reputation: 159331

How to stop a Python script without error messages on the shell?

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

Upvotes: 0

Views: 11518

Answers (1)

Gonzalo Quero
Gonzalo Quero

Reputation: 3323

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)

Upvotes: 9

Related Questions