sys.exit and exceptions (newbie question)

Victor Muslin victor at prodigy.net
Thu Feb 1 18:18:38 CET 2001


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. Of course it is possible to specifically catch SystemExit,
as in the following example, but it is a pain. Moreover, one may want
to exit in a code that plugs into somebody else's code framed by try:
except: blocks.

import sys
try:
	print 'Exiting...'
	sys.exit(0)
except SystemExit:
	print 'System exit...'
except:
	print 'Oops... except'



More information about the Python-list mailing list