Sorting out which exceptions to handle

Derek Fountain nomail at hursley.ibm.com
Tue Nov 4 20:52:21 EST 2003


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

:o) 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.

>> 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.

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!




More information about the Python-list mailing list