Sorting out which exceptions to handle

Mel Wilson mwilson at the-wire.com
Mon Nov 3 23:11:07 EST 2003


In article <3fa70fe4$0$1740$5a62ac22 at freenews.iinet.net.au>,
Derek Fountain <nomail at hursley.ibm.com> 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
>
>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.

   Since the inheritance is that way, I believe catching
StandardError will catch all of them.  Trickier subsest can
be caught by, for example:

        except (InterfaceError, InternalError), details

to get InterfaceError (and all its descendants, if any) and
InternalError sepcifically.

[ ... ]
>How do I trap all the database associated errors this module can generate?

   Probably by trapping the common ancestor, DatabaseError.

        Regards.        Mel.




More information about the Python-list mailing list