sys.exit versus raise SystemExit

Duncan Booth duncan.booth at invalid.invalid
Mon Jan 8 18:48:23 CET 2007


Will McGugan <will at willNOmcguganSPAM.com> wrote:

> wittempj at hotmail.com wrote:
>> 
>> sys.exit() raises a SystemExit, see
>> http://docs.python.org/lib/module-sys.html
>> 
> Oh I know. I was just wondering if there was some sort of subtle 'best 
> practice' recommendation that I wasnt aware of for using sys.exit over 
> raising the exception manually. In the same way that 'open' is prefered 
> over 'file', even though they appear to do the same thing.

'open' is preferred when you are opening a file, 'file' is preferred when 
it is the type you want. That way you leave open the option to intercept 
the 'open' action without interfering with tests against the type.

I guess in a similar way you should prefer the overridable sys.exit() 
function rather than using the exception directly.



More information about the Python-list mailing list