Exceptions and modules / namespaces question
Cliff Wells
logiplexsoftware at earthlink.net
Thu Mar 28 18:22:14 EST 2002
On 28 Mar 2002 15:21:24 -0800
Preston Landers wrote:
> 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.
try:
import module2
module2.raise_foo_exception()
except module2.FooException, e:
print "Got it!"
except:
xi = sys.exc_info()
print "Didn't get it. Got this instead:", xi[0], xi[1]
--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308 (800) 735-0555 x308
More information about the Python-list
mailing list