How to terminate a main script?

Fredrik Lundh fredrik at pythonware.com
Tue Jul 11 04:21:57 EDT 2006


Helmut Jarausch wrote:

> Using sys.exit(0) produces an error
> message which looks dangerous to an
> uninitiated user.

sys.exit(0) doesn't print anything at all.

$ python
>>> import sys
>>> sys.exit(0)
$

however, sys.exit() raises an exception to tell the runtime that it wants
to terminate the program, so if you're using a catch-all exception handler
that treats all exceptions as dangerous errors, it'll look like a dangerous
error too.

the solution is simple: don't do that.  instead of writing:

    try:
        ...
    except:
        print "OMG! Ponies!"

write

    try:
        ...
    except (SystemExit, KeyboardInterupt):
        raise
    except:
        print "OMG! Ponies!"

</F> 






More information about the Python-list mailing list