sys.exit and exceptions (newbie question)
Ben Hutchings
ben.hutchings at roundpoint.com
Thu Feb 1 16:41:31 EST 2001
victor at prodigy.net (Victor Muslin) writes:
> What is a pythonic idiom for forcing an immediate program termination?
> sys.exit() does not do it, because it throws a SystemExit exception,
> so if it is called within a try block, a generic except block is still
> executed.
I'm not sure that there is a Pythonic idiom for forcing immediate
termination, since Python (a) has a pretty good exception system and
(b) does not allow you to get into the sort of mess that you can do
with C, where a clean exit becomes impossible.
An except block that catches all exceptions should always re-raise the
exceptioneal with. If it fails to re-raise
KeyboardInterrupt, for example, the program won't be interruptible by
the user. In the more general case, failing to re-raise exceptions
can be a good way to hide bugs.
You may find the os.abort() or the os._exit() function useful.
--
Any opinions expressed are my own and not necessarily those of Roundpoint.
More information about the Python-list
mailing list