try exec

Afanasiy abelikov72 at hotmail.com
Thu Jan 16 16:24:02 EST 2003


Some code I "exec" throws a TypeError exception. I want it to throw this
exception but want to be able to catch this along with the others which
are in fact being caught by the try/except block.

The specific TypeError in this case is caused by a module-defined
function being called with the incorrect number of arguments. Again,
I want to catch this exception. The problem is, this exception, unlike
the others, is not being caught/handled by my try/except block.

Can someone tell me why this is and perhaps how I can catch it?

i=0
for line, expecting in lines:
  i+=1
  print '[%d] %s' % (i, line)
  try:
    exec line
    if expecting==0:
      printerror('this should have thrown an exception')
  except:
    print '\t '+repr(sys.exc_value.args)
    if expecting==1:
      printerror('this should not be an exception')  

---

The structure of "lines" is :

lines = [
  ('foo = MyModule.Something()',1),
  ('foo.Function("hmm")',0),
  ...etc..
]





More information about the Python-list mailing list