exceptions from modules

Matthew D. Wood woodm at equire.com
Fri Aug 24 01:30:27 CEST 2001

Ok, I posted half of this earlier, but something strange happend, so I
will try again.  If there is a double post, I appologize.

I have a main program that imports a user-written module.

In that module, I want to raise a user-defined exception.

The try-except block is in the main program.

Normally, I would think of writing everything like this:

(somewhere deep inside the __main__ jungle...)

except my_module.my_exception, value :
    print 'yippie'

However, I have seen PLENTY of examples of modules that seemingly defy
scoping rules in the way they present their exceptions to the world. 
And instead you can write:

except my_exception, value :

How do they do that?  Where is the exception defined?  

   I don't understand the 'global' key-word.
   I don't believe in "from blah import *" so it's definitely not
   I assume that the my_exception class inherits from Exception (or
one of it's children)

Now even worse is that I have 2 code examples, one that just works,
and one that can't find the exception.  I don't understand.  I have
banged my head against the wall A LOT on this one.


More information about the Python-list mailing list