How to except the unexpected?

Rene Pijlman at
Sat Mar 4 12:43:26 CET 2006

Peter Hansen:
>Good code should probably have a very small set of real exception 
>handling cases, and one or two catchalls at a higher level to avoid 
>barfing a traceback at the user.

Good point.

>> A catchall seems like a bad idea, since it also catches AttributeErrors
>> and other bugs in the program.
>Generally speaking this won't be a problem if you have your catchalls at 
>a fairly high level and have proper unit tests for the lower level code 
>which is getting called.  You are doing unit testing, aren't you? ;-)

With low coverage, yes. But unit testing isn't the answer for this
particular problem. For example, yesterday my app was surprised by an
httplib.InvalidURL since I hadn't noticed this could be raised by
robotparser (this is undocumented). If that fact goes unnoticed when
writing the exception handling, it will also go unnoticed when designing
test cases. I probably wouldn't have thought of writing a test case with a
first url with some external domain (that triggers robots.txt-fetching)
that's deemed invalid by httplib.

René Pijlman

More information about the Python-list mailing list