Python style: exceptions vs. sys.exit()

Christian Heimes lists at cheimes.de
Tue Sep 23 18:10:26 EDT 2008


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?

Side note:

sys.exit() is just another way to write raise SystemExit. The function 
is defined as:

static PyObject *
sys_exit(PyObject *self, PyObject *args)
{
         PyObject *exit_code = 0;
         if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
                 return NULL;
         /* Raise SystemExit so callers may catch it or clean up. */
         PyErr_SetObject(PyExc_SystemExit, exit_code);
         return NULL;
}

Christian




More information about the Python-list mailing list