How do I catch ExpatError exception?

Steve Holden steve at holdenweb.com
Wed Aug 16 20:40:07 EDT 2006


Shuaib wrote:
> Hey!
> 
> I am getting this exception.
> 
> xml.parsers.expat.ExpatError
> 
> But I am not able to catch it with "except
> xml.parsers.expat.ExpatError:" It says "NameError: global name 'xml' is
> not defined".
> 
> I am also not able to catch it with "except ExpatError:" Gives
> "NameError: global name 'xml' is not defined"
> 
> How do I catch it? (I am parsing an xml file)
> 
import xml.parsers.expat

That way the interpreter will be able to resolve the reference to the 
exception.

> Also, how do I get the line number where an exception was thrown?
> 
#
# Exception traceback test
#
def r():
   return 1/0

try:
   print r()
except:
   import sys
   t, v, tb = sys.exc_info()

print "exception on line", tb.tb_lineno

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list