Python style: exceptions vs. sys.exit()

Steven D'Aprano steven at
Wed Sep 24 08:17:04 CEST 2008

On Tue, 23 Sep 2008 13:25:26 -0700, Drake wrote:

> I have a general question of Python style, or perhaps just good
> programming practice.
> My group is developing a medium-sized library of general-purpose Python
> functions, some of which do I/O. Therefore it is possible for many of
> the library functions to raise IOError Exceptions. The question is:
> should the library function be able to just dump to sys.exit() with a
> message about the error (like "couldn't open this file"), or should the
> exception propagate to the calling program which handles the issue?
> Thanks in advance for anyone who can either answer my question or point
> me to where this question has already been answered.

Presumably somebody has suggested that calling sys.exit() was a good 
option. I'm curious to what possible reason they could give for such a 
poor choice.

Hint: if a library function can't open a file, the application should be 
given the opportunity to open a different file. Or do something 
completely different instead. Whatever. That's not up to the library to 
decide, not even if the library is in such a broken state that it can't 
continue. Why not? Because the application might deal with that by 
unloading the library and continuing regardless.


More information about the Python-list mailing list