Exception problems

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Dec 4 19:28:39 EST 2002


On Wednesday 04 December 2002 15:38, Richard Jones wrote:
> I've just noticed that exceptions aren't caught in the following
> situation:

[snip]

You need to post code instead of a vague description of what you 
tried to do.  Otherwise, we can only guess at your meaning.

Here is some code which demonstrates what I think you mean :

a.py
~~~~
import b

class FooExc: pass

def bar():
    print "a: id( FooExc ) == ", id( FooExc )
    try:
        b.f()
    except FooExc:
        print "Trapped FooExc as planned"

if __name__ == '__main__':
    import sys
    print sys.version
    print
    bar()
~~~~

b.py
~~~~
from a import FooExc

def f():
    print "b: id( FooExc ) == ", id( FooExc )
    raise FooExc
~~~~

I'll skip the output, but the printouts indeed show different id 
numbers.  I don't think it is a bug, but I don't have the 
explanation at hand (others do, I'm sure).

The solution is to make c.py, and have it contain the class FooExc, 
and let a.py and b.py both import it.  Avoid circular imports at 
(almost) all costs, and you will lead a happier life.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
cnetzer at mail.arc.nasa.gov




More information about the Python-list mailing list