Identifying exceptions that can be raised

Peter Hansen peter at
Mon Nov 22 04:16:46 CET 2004

Dan wrote:
> the thought occured to me that if I _did_ want to handle exceptions, 
> I would have no idea what the possibilities even were.

Here's another area where good unit testing can help (apologies
to those with an irrational negative reaction to test-driven
development concepts).  By writing tests which exercise the routines
you are calling in various ways, you will often find which
exceptions are of interest but not perhaps documented clearly.

For example, the other day I discovered that if you attempt
to read or write from/to a closed file, you don't get what
you might expect (an IOError or an OSError), but in fact
you get a ValueError.  Writing unit tests was what led to
this discovery.  (Perhaps it's actually documented... I don't
know and didn't bother looking.)

 >>> f = open('test', 'w')
 >>> f.close()
 >>> f.write('testing')
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ValueError: I/O operation on closed file


More information about the Python-list mailing list