[Python-Dev] When to use EOFError?

Nikolaus Rath Nikolaus at rath.org
Mon Jun 27 12:09:25 EDT 2016


On Jun 21 2016, Serhiy Storchaka <storchaka at gmail.com> 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?

I think EOFError conveys more information. UnpicklingError can mean a
lot of things, EOFError tells you the precise problem: pickle expected
more data, but there was nothing left. I think in doubt the more
specific exception (in this case EOFError) should be raised.

Best,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-Dev mailing list