Identifying exceptions that can be raised

Peter Hansen peter at engcorp.com
Tue Nov 23 02:25:53 CET 2004


Jim Hefferon wrote:
> Peter Hansen <peter at engcorp.com> wrote ..
> 
>>The usual approach is to have a caller catch exceptions
>>*which it is capable of dealing with appropriately*, and
>>ignore others.  In some cases it's necessary to have a
>>"generic" exception handler, such as at the top level of
>>a thread, to catch and log all exceptions, but that's
>>relatively rare.
> 
> I wonder if perhaps you don't write different kinds of programs than I
> do.

I certainly do.  I don't, for example, ever do CGI...

> For instance, I have a cgi program.  One routine will get an id from a
> httpd parameter, use it to reach into a database and fetch some xml,
> and then pull information out of the xml for display.  That I can
> tell, there are about 100 exceptions that could be raised ...

...but were I faced with this situation, I would follow my
above advice about "in some cases it's necessary...".  This
is exactly such a situation.  I would always wrap such a
program** at the top level with a generic exception handler
and a call to traceback.format_exception() (or whatever it's
called), turning any errors into visible results, or at
the very least logging them somewhere for analysis.

** I don't think of CGI calls as independent _programs_, mind
you, any more than I think of individual threads as programs,
but I'm not sure this is affecting my opinion here vis-a-vis
exception handling.

-Peter



More information about the Python-list mailing list