An unhandled exception immediately stops the execution of your code.

A handled exception (try/except) does not stop code execution (unless
you explicitly tell it to).

This shows how a handled exception does not stop code execution:

try:
        raise Exception
except:
        print 'caught exception'
print 'fell through'


Hope this helps...

-Scott

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Jan Eden
Sent: Tuesday, December 06, 2005 10:24 AM
To: Pawel Kraszewski; tutor@python.org
Subject: Re: [Tutor] Exception repeated in a loop

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 ~/
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to