Dealing with exceptions

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Mar 2 14:18:16 EST 2013


On 02/03/2013 17:58, Ian Kelly wrote:
> On Sat, Mar 2, 2013 at 10:40 AM, bvdp <bob at mellowood.ca> wrote:
>> Every time I write a program with exception handling (and I suppose that includes just about every program I write!) I need to scratch my brain when I create try blocks.
>>
>> For example, I'm writing a little program do copy specific files to a USB stick. To do the actual copy I'm using:
>>
>>      try:
>>         shutil.copy(s, os.path.join(usbpath, songname))
>>       except ...
>>
>> now, I need to figure out just what exceptions to handle. Let's see:
>>
>>    IOError  that means that the disk is full or otherwise buggered. Better dump out of the loop.
>>
>> But, I know there can be other errors as well. Doing some tests, I know that certain filenames are invalid (I think a "?" or unicode char is invalid when writing to a FAT32 filesystem). And, so what exception is that? Without actually creating the error, I can't figure it out.
>
> OSError.  In Python 3, I expect it would more specifically be a
> FileNotFoundError, which is a subclass of OSError.

This wasn't introduced until Python 3.3 see 
http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy

>
>> In this case, I can run the program an number of times and parse out the errors and write code to catch various things. But, I think I'm missing something completely. Guess what I'm looking for is a list of possible (probable?) errors for the shutil.copy() command. And, in a much bigger manual, for most other commands.
>
> OSError will cover a wide swath of possible exceptions here.
>


-- 
Cheers.

Mark Lawrence




More information about the Python-list mailing list