[Tutor] Exception repeated in a loop

Jan Eden lists at janeden.org
Tue Dec 6 17:24:21 CET 2005


Hi Pawel,

Pawel Kraszewski wrote on 06.12.2005:

>Dnia wtorek, 6 grudnia 2005 16:29, Jan Eden napisa?:
>> Hi,
>>
>> I use the following loop to parse some HTML code:
>>
>> for record in data:
>>     try:
>>         parser.feed(record['content'])
>>     except HTMLParseError, (msg):
>>         print "!!!Parsing error in", record['page_id'], ": ", msg
>>
>> Now after HTMLParser encounters a parse error in one record, it repeats to
>> execute the except statement for all following records - why is that?
>
>Short answer: because you told Python to do so...
>
>Long answer:
>
>My hint for students having such problems is to execute their code with a 
>pencil on a hardcopy. They read aloud what the program currently does  - 
>usually they spot the error during the first "reading".
>
>Your code being "read loud"
>
>1. begin loop
>2.  attempt to execute parser.feed
>3.   abort attempt if it fails, showing the error
>4. take next loop
>
>So - you take next loop regardless of the failure or not. There are two ways 
>out of here. I wrote them "aloud", to transcribe into python as an excersize:
>
>(Notice the difference between this and your original)
>
>I)
>
>1. attempt to 
>2.  begin loop
>3.   abort attempt if it fails, showing the error
>4.  take next loop
>
>II)
>1. begin loop
>2.  attempt to execute parser.feed
>3.   abort attempt if it fails, showing the error AND breaking the loop
>4. take next loop
>
Thanks, I tested your suggestion, which works fine. But I don't understand the problem with my original code.

If the parser raises an exception for a certain record, it should print the error message and move on to the next record in the loop. Why would I need the break statement? What's more - if the break statement is executed, all following records will never be parsed.

I still don't understand why failure of a single record affects the other records.

Thanks,

Jan
-- 
There's no place like ~/


More information about the Tutor mailing list