Sorting out which exceptions to handle

D

Derek Fountain

I recently asked a question about the exceptions raised by the MySQLdb
module. The answer produced a new question. :eek:) 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?
 
F

Francis Avila

Derek Fountain said:
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.

The O'Reilly book is simply pointing out a convention. Usually modules only
need one exception type, and it's called 'Error' (or 'error' for builtin
modules). It's not some magic syntax or semantics that catches every kind
of exception that a module could raise.
How do I trap all the database associated errors this module can generate?

Trapping an exception traps all exceptions which are subclasses of that
exception.

E.g.:
.... pass
........ pass
........ raise SpecialError #Implicit class instantiation
.... except BaseError, err:
.... print 'Caught instance of BaseError or subclass thereof.'
.... print 'Specifically,', repr(err)
....

Caught instance of BaseError or subclass thereof.

Hope this helps.
 
A

Alex Martelli

Derek said:
I recently asked a question about the exceptions raised by the MySQLdb
module. The answer produced a new question. :eek:) 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.


Alex
 
D

Derek Fountain

Well, yes, generally. Not _invariably_, else I would have written

:eek:) Fair point. I took the "generally" bit to mean the module name (printed
in italics) was general and the "Error" (printed in normal text) was
specific. Obviously the "Error" is somewhat general too.

I'm aware that I'm actually using your book as a learning guide, rather than
a reference, which isn't too fair on it. The odd daft question and bit of
confusion aside, it's working quite well for me though.
What errors? this seems exactly correct (save that you won't catch
*warnings*) according to the above hierarchy.

Er, that's odd. I tried it again and now it works. I know I wasn't imagining
it though, because I can scroll back in the console buffer and see the
error I got:

File "./model/DBConnection.py", line 22, in connect
except MySQLdb.Error, dbErr:
NameError: global name 'MySQLdb' is not defined

This suggests I hadn't imported the MySQLdb module, but I know I had. When I
changed it to:

except OperationalError, err:

and put a:

import MySQLdb

next to the existing:

from MySQLdb import *

at the top of the file, the error messages went away. I now take out the
"import MySQLdb" and make it "except MySQLdb.StandardError, err:" and
everything is happy.

<blank look!>

Oh well, it works now. Thanks for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top