How to Determine The Module a Particular Exception Class is Defined In

Posted by doug on Stack Overflow See other posts from Stack Overflow or by doug
Published on 2009-12-10T05:31:17Z Indexed on 2010/04/04 20:43 UTC
Read the original article Hit count: 526

Note: i edited my Q (in the title) so that it better reflects what i actually want to know. In the original title and in the text of my Q, i referred to the source of the thrown exception; what i meant, and what i should have referred to, as pointed out in one of the high-strung but otherwise helpful response below, is the module that the exception class is defined in. This is evidenced by the fact that, again, as pointed out in one of the answers below the answer to the original Q is that the exceptions were thrown from calls to cursor.execute and cursor.next, respectively--which of course, isn't the information you need to write the try/except block.

For instance (the Q has nothing specifically to do with SQLite or the PySQLite module):

from pysqlite2 import dbapi2 as SQ

try:
    cursor.execute('CREATE TABLE pname (id INTEGER PRIMARY KEY, name VARCHARS(50)')
except SQ.OperationalError:
    print("{0}, {1}".format("table already exists", "... 'CREATE' ignored"))
#
cursor.execute('SELECT * FROM pname')
while 1:
    try:
        print(cursor.next())
    except StopIteration:
        break
#

i let both snippets error out to see the exception thrown, then coded the try/finally blocks--but that didn't tell me anything about which module the exception class is defined. In my example, there's only a single imported module, but where there are many more, i am interested to know how an experienced pythonista identifies the exception source (search-the-docs-till-i-happen-to-find-it is my current method).

[And yes i am aware there's a nearly identical question on SO--but for C# rather than python, plus if you read the author's edited version, you'll see he's got a different problem in mind.]

© Stack Overflow or respective owner

Related posts about python

Related posts about exception-handling