Exceptions and modules / namespaces question

Emile van Sebille emile at fenx.com
Thu Mar 28 18:57:15 EST 2002


Preston Landers
> 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.
>

It seems to me that you have a __main__ vs module1 issue.  When module2
imports module1, it gets a new copy, not __main__.

> -------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

if you import module1 here


>        module2.raise_foo_exception()
>    except FooException, e:

then except module1.FooException

it may work.  Better perhaps to define your exceptions in a separate
module and import it into both.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list