I've inherited a code base with a lot of try ... except:
clauses. Most of them are way too broad, and make it a pain to debug. I've been going through and changing each to the most reasonable form, which usually involves removing it or specifying the exception(s).
But I'm a little stumped by this one:
try:
with open(self.session_filename, "rb") as f:
data = cPickle.loads(zlib.decompress(f.read()))
except:
# we didn't need your file anyway!
return
I want to handle the exceptions specifically, but the Python docs on Pickle say:
exception pickle.UnpicklingError
This exception is raised when there is a problem unpickling an object. Note that other exceptions may also be raised during unpickling, including (but not necessarily limited to)
AttributeError
,EOFError
,ImportError
, andIndexError
.
Translation: The method might throw anything!
So apparently it can raise arbitrary exceptions, which makes it hard to handle them specifically.
How can I most reasonably deal with this situation, keeping the following goals in mind:
It may be that not all can be fulfilled.
Breaking up the exceptions into several handlers may be what you want, with a catchall if you need it.
try:
with open(self.session_filename, "rb") as f:
data = cPickle.loads(zlib.decompress(f.read()))
except pickle.UnpicklingError as e:
# normal, somewhat expected
continue
except (AttributeError, EOFError, ImportError, IndexError) as e:
# secondary errors
print(traceback.format_exc(e))
continue
except Exception as e:
# everything else, possibly fatal
print(traceback.format_exc(e))
return
Generally I would try to handle only specific exceptions which is not really possible in this case: Who knows what exceptions could cPickle.loads
throw in the future.
Where it is no possible to be more specific in the type of the exceptions I'd try to be more specific on the code block that I wrap in try ... except
. For this example this could be like this:
with open(self.session_filename, "rb") as f:
uncompressed_data = zlib.decompress(f.read())
try:
data = cPickle.loads(uncompressed_data)
except cPickle.UnpicklingError:
raise
except Exception as e:
# Unpickling failed
raise cPickle.UnpicklingError(repr(e))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With