How to use self-inspection to check for try-block

elmar at elmar at
Fri Mar 20 09:32:12 CET 2009

Hi everyone,

is there a sufficiently easy possibility for a Python function to find
out whether it has been called from a try-block or not?

  print "Calling foo"
  print "Got exception"

In the example above, foo() should be able to 'see' that it was called
from a try block, allowing it to behave differently.

Can this information be obtained from the traceback/frame/code
objects, or is that too difficult?

Many thanks for your help,

More information about the Python-list mailing list