sys.exit and exceptions (newbie question)

Greg Jorgensen gregj at pobox.com
Thu Feb 1 14:28:43 EST 2001


Functions that exit without giving the caller a chance to catch the
exception are a very bad idea. Exception handling is good Python
practice; forcing an un-catchable exit is not.


In article <3a7998cf.869169328 at localhost>,
  victor at prodigy.net (Victor Muslin) wrote:
> 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'
>

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list