On Tue, Jun 21, 2016, at 16:48, Serhiy Storchaka wrote: > There is a design question. If you read file in some format or with some > protocol, and the data is ended unexpectedly, when to use general > EOFError exception and when to use format/protocol specific exception? > > For example when load truncated pickle data, an unpickler can raise > EOFError, UnpicklingError, ValueError or AttributeError. It is possible > to avoid ValueError or AttributeError, but what exception should be > raised instead, EOFError or UnpicklingError? Maybe convert all EOFError > to UnpicklingError?
I think this is the most appropriate. If the calling code needs to know the original reason it can find it in __cause__. My instinct, though, (and I'm aware that others may not agree, but I thought it was worth bringing up) is that loads should actually always raise a ValueError, i.e. my mental model of loads is like: def loads(s): f = BytesIO(s) try: return load(f) except UnpicklingError as e: raise ValueError from e _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com