[Tutor] Trying to catch an exception...
Kent Johnson
kent37 at tds.net
Sat Sep 16 05:58:22 CEST 2006
William O'Higgins Witteman wrote:
> I am running a program (via py2exe) that is reading some XML files with
> xml.minidom. I am getting an error wherein I pull a value from a tag
> which (I think) happens to be empty. Thus, it throws this error:
>
> AttributeError: 'NoneType' object has no attribute 'data'
>
> Here's the code that creates this problem:
>
> def functionname(fileobject):
> try:
> xmldoc = minidom.parse(fileobject)
> except xml.parsers.expat.ExpatError, AttributeError:
This should be written,
except (xml.parsers.expat.ExpatError, AttributeError):
The parentheses are important. The way you have written it, an instance
of ExpatError, if it occurs, will be bound to the name AttributeError;
AttributeErrors will not be caught at all.
Kent
> logit = "There is a malformed file: " + fileobject + "\n"
> logfile.write(logit)
> else:
> a = xmldoc.getElementsByTagName('date_modified')
> try:
> b = a[0].firstChild.data
> except xml.parsers.expat.ExpatError, AttributeError:
> logit = "There is a malformed file: " + fileobject + "\n"
> logfile.write(logit)
>
> I am wondering what I have to do to catch this exception - I'm assuming
> that the problem is that "a" is an empty object, and so it has not
> attributes. Thanks.
More information about the Tutor
mailing list