Sorting out which exceptions to handle

Derek Fountain nomail at hursley.ibm.com
Mon Nov 3 21:36:19 EST 2003


I recently asked a question about the exceptions raised by the MySQLdb
module. The answer produced a new question. :o) The documentation I was
pointed at tells me the module has the following exception tree:

StandardError
        |__Warning
        |__Error
           |__InterfaceError
           |__DatabaseError
              |__DataError
              |__OperationalError
              |__IntegrityError
              |__InternalError
              |__ProgrammingError
              |__NotSupportedError

I want to catch all of them! How can I specify that set of exceptions, but
no others? I could type them all into one line, but that doesn't make my
code future proof - new exceptions could be added to the module.

The O'Reilly Python in a Nutshell book says "Generally your code uses a
statement of the form:

try:
  ...
except module.Error, err:
  ...
"

with the word "module" in italics. I tried the line:

except MySQLdb.Error, err:

and various variations, but always got errors.

How do I trap all the database associated errors this module can generate?






More information about the Python-list mailing list