handling ExpatError exception raised from ElementTree.XML() method
mirandacascade at yahoo.com
mirandacascade at yahoo.com
Thu Oct 27 00:15:20 EDT 2005
Verion of Python: 2.4
O/S: Windows XP
ElementTree resides in the c:\python24\lib\site-packages\elementtree\
folder
When a string that does not contain well-formed XML is passed as an
argument to the XML() method in ElementTree.py, an ExpatError exception
is raised. I can trap the exception with a try/except where the except
does not specify a specific exception, but I cannot figure out how to
construct the except clause in a try/except statement to catch the
ExpatError exception. Interactive window illustrates
>>> import elementtree.ElementTree as ElemTree
>>> badxml = '<response>abc</responsez>'
>>> root = ElemTree.XML(badxml)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
960, in XML
parser.feed(text)
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
1242, in feed
self._parser.Parse(data, 0)
ExpatError: mismatched tag: line 1, column 15
>>> try:
... root = ElemTree.XML(badxml)
... except:
... print 'some exception raised'
...
some exception raised
>>> try:
... root = ElemTree.XML(badxml)
... except ExpatError:
... print 'ExpatError exception raised'
...
Traceback (most recent call last):
File "<interactive input>", line 3, in ?
NameError: name 'ExpatError' is not defined
I'm guessing that I need to define/describe the ExpatError exception
class and then refer to that defined exception class after the keyword
'except' and before the ':', but I cannot figure out how to do that.
More information about the Python-list
mailing list