Dealing with exceptions

Steven D'Aprano steve+comp.lang.python at
Sun Mar 3 02:41:20 CET 2013

On Sat, 02 Mar 2013 11:35:13 -0800, bvdp wrote:

>> IOError and OSError should cover all copy problems, I think.
> How do you know that? I can figure it out as well by running the
> program, but I'd like to make the determination of what to catch when
> I'm writing the code.

In my experience, I would say:

20% by reading the documentation, 20% by experience, and 60% by 
experimentation at the interactive interpreter.

Generally if you read the docs, you will get some idea of the exceptions 
you can expect to get. E.g. the docs for open claim:

"Raise IOError upon failure."

(call "help(open)" in the interactive interpreter).

Experience and experimentation come into it in the (unfortunately very 
common case) where the docs don't describe the exceptions you can expect.

For the specific case of IOError and OSError, another very useful skill 
is googling for the specific errno you can test for:

except IOError as e:
    if e.errno == whatever:

In Python 3.3, this becomes much nicer with individual exceptions for the 
most common errnos.


More information about the Python-list mailing list