[Tutor] a graceful program exit

Chris Fuller cfuller084 at thinkingplanet.net
Thu Aug 12 08:59:26 CEST 2010

The preferred and most graceful way is to use neither, as others have pointed 
out.  However, you can't return an exit code that way, and it sometimes isn't 
feasible to structure your code to allow it.  I prefer SystemExit, because it 
doesn't require adding something to the namespace (the sys module), and it 
seems a bit more pythonic.  You can trap it, however, which bugs me.  I think 
these two should exhibit the same behavior.  I'd use sys.exit for "real" code, 
even though it appeals less aesthetically, because it is less prone to doing 
something unexpected (one of Python's guiding principles, btw).


On Wednesday 11 August 2010, Bill Allen wrote:
> I have only learned a couple of ways to cause a Python program to exit:
> sys.exit(0)  &  raise.SystemExit .  I am using this in a try/except block.
> Which is preferred?  Are there other, better ways?
> Thanks,
> Bill Allen

More information about the Tutor mailing list