Exceptions and modules / namespaces question
Preston Landers
pibble at yahoo.com
Thu Mar 28 18:21:24 EST 2002
This is a question about modules / namespaces and exceptions.
It's probably been asked before, but.... I looked in the Python FAQ
and did some searches on Google (& Groups) and didn't turn up
anything.
I've run into this thing a couple of times before, but I was always
able to work around it or avoid it, but I'd like to understand what's
going on here, because it's probably just a problem with my
understanding.
The problem, in a nutshell, is that I can't seem to catch an exception
in the same module it was defined in if the function that raised the
exception is in a different module, because of naming issues.
-------module2.py:
import module1
def raise_foo_exception():
raise module1.FooException("HELLO")
-------module1.py:
import exceptions, sys
class FooException(exceptions.Exception):
pass
def main():
try:
import module2
module2.raise_foo_exception()
except FooException, e:
print "Got it!"
except:
xi = sys.exc_info()
print "Didn't get it. Got this instead:", xi[0], xi[1]
if __name__ == "__main__":
main()
---------result:
Didn't get it. Got this instead: module1.FooException HELLO
The question is, how do I explicitly catch FooException in main()
without doing a catch-all except: and then examining the type of my
exception, and making a decision based on that. It's kinda clunky.
thanks,
Preston Landers
pibble at yahoo.com
More information about the Python-list
mailing list