Exception problems

Richard Jones rjones at ekit-inc.com
Wed Dec 4 18:38:16 EST 2002


I've just noticed that exceptions aren't caught in the following situation:

. script/module a.py defines an exception FooExc
. a.py imports module b.py
. b.py has function f() which imports FooExc from a.py
. if f() raises FooExc, a.py can't trap the exception - not as:
    except FooExc:
  or 
    except 'FooExc':

If I instead import a.py and cause f() to run, then the exception will be 
caught as expected in a.py.

I notice that the id() of the exception class in a.py as-a-script differs from 
the id() in b.py. But the id() of the exception class in a.py as-a-module is 
the same as the id() in b.py in that instance. The exception "name" also 
differs - in a.py as-a-script, it's "__main__.FooExc" and in a.py as-a-module 
and b.py it's "a.FooExc".

Is this a bug or a feature? Just curious. Even though the names are different, 
I would've expected the classes to have the same id()...


    Richard





More information about the Python-list mailing list