handling ExpatError exception raised from ElementTree.XML() method

Paul Boddie paul at boddie.org.uk
Thu Oct 27 11:00:57 EDT 2005


mirandacasc... at yahoo.com wrote:
>
> 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.

You just need to know how to reference ExpatError yourself. Doing a
grep on the site-packages directory revealed that the most likely
location of the ExpatError class is xml.parsers.expat. So, first you
need to import that package:

import xml.parsers.expat

Then, you can refer to the class as an attribute of that package.
Alternatively...

from xml.parsers.expat import ExpatError

...lets you use the name directly.

I think that it can be useful when making a library to make common
exception classes attributes of certain principal classes/objects, in
order to simplify this kind of situation and to avoid the need to find
the originating module of some exception or other.

Paul




More information about the Python-list mailing list