Sorting out which exceptions to handle

Alex Martelli aleax at
Tue Nov 4 16:35:47 CET 2003

Derek Fountain wrote:

> 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
> The O'Reilly Python in a Nutshell book says "Generally your code uses a
> statement of the form:
> try:
>   ...
> except module.Error, err:
>   ...
> "

Well, yes, generally.  Not _invariably_, else I would have written that;-).

> with the word "module" in italics. I tried the line:
> except MySQLdb.Error, err:
> and various variations, but always got errors.

What errors?  this seems exactly correct (save that you won't catch
*warnings*) according to the above hierarchy.  If you also need to
catch warnings together with errors,

except MySQLdb.StandardError, err:

should be satisfactory.


More information about the Python-list mailing list