try... except with unknown error types

Ethan Furman ethan at stoneleaf.us
Sun Aug 21 15:22:47 EDT 2011


Paul Rubin wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>>> But there's no way to know what that minimum is.  Python libraries throw
>>> all sorts of exceptions that their documentation doesn't mention.
>> Yes, you're absolutely correct. But it's also irrelevant. Most of those
>> exceptions should not be caught, even if you know what they are, because
>> they represent either bugs that should be fixed, or bad data which should
>> raise an exception. A bare except, or except Exception, is hardly ever the
>> right approach.
> 
> I'm not sure what to do instead.  The exceptions I'm currently dealing
> with happen when certain network operations go wrong (e.g. network or
> remote host is down, connection fails, etc.)  The remedy in each case is
> to catch the exception, log the error, and try the operation again
> later.  But there's no guaranteed-to-be-complete list in the Python docs
> of all the exceptions that can be thrown.  A new and surprising mode of
> network failure can lead to an unhandled exception, unless you catch
> everything.

In a case like this I can see catching everything so long as (which you 
say you are doing) you log the error somehow -- what's really 
frustrating is when the error is simply tossed with no record 
whatsoever... what a pain to debug!

~Ethan~



More information about the Python-list mailing list