exceptions and items in a list

rbt rbt at athop1.ath.vt.edu
Mon Jan 10 16:37:08 EST 2005


Andrey Tatarinov wrote:
> rbt wrote:
> 
>> If I have a Python list that I'm iterating over and one of the objects 
>> in the list raises an exception and I have code like this:
>>
>> try:
>>     do something to object in list
>> except Exception:
>>     pass
>>
>> Does the code just skip the bad object and continue with the other 
>> objects in the list, or does it stop?
> 
> 
> # skip bad object and continue with others
> for object in objects:
>     try:
>         #do something to object
>     except Exception:
>         pass
> 
> # stop at first bad object
> try:
>     for object in objects:
>         #do something to object
> except Exception:
>     pass

Thanks Andrey. That's a great example of how to do it.



More information about the Python-list mailing list