When is bare except: justified?

Peter Hansen peter at engcorp.com
Fri Dec 5 12:56:27 EST 2003


"John J. Lee" wrote:
> 
> Bare "except:", with no exception specified, is nasty because it can
> easily hide bugs.
[snip remainder of excellent comments]

I believe the only case where we permit "bare" except statements is when
there's a clear intention that we want higher level code to be immune to
problems in the lower level code, primarily because the higher level code
makes up part of a long-running process.

In a nutshell, if we have one of our multi-threaded embedded applications,
it's more important that the program as a whole keep running than it is
to expose potentially minor problems by crashing the entire system with a
nasty traceback message exposed to the user.

Since most of the lower level code has decent unit tests, and we have
acceptance tests covering the combination of it plus higher level code, 
we tend to accept that the basic functionality of the system is already
guaranteed, and therefore any unhandled exceptions would represent things
which it's not worth shutting down over.  We do log them in a log file,
but we then ignore the error and continue running, or restart the thread,
as appropriate.

In all other cases, it's an unwritten rule that bare except is a sign of
poorly written code.

(I may have overlooked a special case or two where we would make an
exception, no pun intended... not sure.)

-Peter




More information about the Python-list mailing list