try... except with unknown error types
no.email at nospam.invalid
Sun Aug 21 20:26:51 CEST 2011
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
The Erlang approach is tempting. Don't catch the exception at all--just
let the process crash, and restart it. But that's a more heavyweight
operation in Python.
> After all, unless you're writing
> software for a nuclear reactor, or an aeroplane's autopilot, chances are
> that *bugs don't really matter*. That is to say, if you release software
> with a hidden bug, the consequences generally aren't very important.
It's a retail application that would cause some business disruption and
a pissed off customer if the program went down. Also it's in an
embedded box on a customer site. It's not in Antarctica or anything
like that, but it's a few towns over, and someone would have to drive
there (probably through heavy traffic) if something went wrong that
power cycling the box couldn't fix.
> It took me a long time to realise that the world won't end if I write a
> piece of software with a bug.
It's not the end of the world if I get run over a truck, but such an
event would be of enough consequence to me personally that I find it
worth going to some trouble to avoid it.
More information about the Python-list