Click to See Complete Forum and Search --> : Python Exception Handling


abszeph
04-20-2003, 10:26 PM
I've got a question about exception handling.

Is there any way to clear the error?

I've got some error handling code in a for loop, and I can't figure out how to clear the error state.


for file in os.listdir('mydir'):
file = "mydir/%s" % file
print "Processing %s" % file
f = open(file, 'r')

#grab the documents
try:
pURL.feed(f.read())
pURL.close()
except sgmllib.SGMLParseError:
continue


One malformed document causes the rest of the directory to get skipped. Any ideas?

Thanks...

GaryJones32
04-20-2003, 11:51 PM
i don't think i ever tried to use SGML so i don't know the specifics..
but it seems like you cold just put

except:
pass

since you are not trying to get anything out of the exception catch ?

since i can't see the rest i don't know but it might be the
continue
statement is
(by going straight back to the top of the loop)
i think that's what it does anyway
is causing the rest of the loop stuff to get passed over
like changing the value of file or whatever

inkedmn
04-21-2003, 12:35 PM
you could also do:


try:
<do stuff>
except Exception, e:
print e
pass


so you at least know that the exception occurred and where.

Stuka
04-21-2003, 01:46 PM
DISCLAIMER: I'm no pythonista.
However, I'd bet that you're issue has to do with your for loop more than anything else - if the try/except is working right, you should go through fine. Check your indentation and such - make sure that you're nesting everything correctly.