try exec

Afanasiy abelikov72 at
Thu Jan 16 22:24:02 CET 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?

for line, expecting in lines:
  print '[%d] %s' % (i, line)
    exec line
    if expecting==0:
      printerror('this should have thrown an exception')
    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),

More information about the Python-list mailing list