Little Help with Exceptions and ConfigParser

M

mwt

Hi -
I'm having a little trouble using exceptions properly. I currently have
an initialization function on a little app that looks like this:

def __init__(self, config_file):
self.fahdata = fahdata.FAHData()
self.INI = ConfigParser.ConfigParser()
if os.path.exists(config_file):
try:
self.INI.read(config_file)
except ConfigParser.ParsingError:
print "Cannot parse configuration file!"

This is just a hack I came up with to load the configuration file and
try to test for serious problems with it. Trouble is, it doesn't really
do that, and furthermore, if there are small problems with the file
(such as a NoSectionError) the whole program bombs. Instead, I would
like to test for the relevant Errors and Exceptions, and have the
program deal with them gracefully.

Would one of you gurus be so kind as to point me in the right direction
here? I know that there are many possibilities, but what would be a
sane and reasonable set of exceptions to throw?

Meanwhile, I will be reading pages 337-339 in my copy of Python in a
Nutshell to try to get clearer about exceptions and how to use them.

Thanks.
mwt
 
M

mwt

Whoops. Those page numbers are from "Cookbook."
As well as the Except section in "Nutshell."
Still stewing away here. ;)
 
K

Kent Johnson

mwt said:
Hi -
I'm having a little trouble using exceptions properly. I currently have
an initialization function on a little app that looks like this:

def __init__(self, config_file):
self.fahdata = fahdata.FAHData()
self.INI = ConfigParser.ConfigParser()
if os.path.exists(config_file):
try:
self.INI.read(config_file)
except ConfigParser.ParsingError:
print "Cannot parse configuration file!"

This is just a hack I came up with to load the configuration file and
try to test for serious problems with it. Trouble is, it doesn't really
do that, and furthermore, if there are small problems with the file
(such as a NoSectionError) the whole program bombs. Instead, I would
like to test for the relevant Errors and Exceptions, and have the
program deal with them gracefully.

All the ConfigParser exceptions are subclasses of ConfigParser.Error so
if you catch that instead of ConfigParser.ParsingError your code will
catch NoSectionError as well.

Kent
 
M

mwt

Would something like this be more useful?

def __init__(self, config_file):
self.fahdata = fahdata.FAHData()
self.INI = ConfigParser.ConfigParser()
if os.path.exists(config_file):
try:
self.INI.read(config_file)
except ConfigParser.Error, err:
print "Cannot parse configuration file. %s" %err
except IOError:
print "Problem opening configuration file. %s" %err
except Error:
print "Problem with with configuration file. %s" %err
 
M

mwt

(Whoops, again.)

def __init__(self, config_file):
self.fahdata = fahdata.FAHData()
self.INI = ConfigParser.ConfigParser()
if os.path.exists(config_file):
try:
self.INI.read(config_file)
except ConfigParser.Error, err:
print "Cannot parse configuration file. %s" %err
except IOError, err:
print "Problem opening configuration file. %s" %err
except Error, err:
print "Problem with with configuration file. %s" %err
 
K

Kent Johnson

mwt said:
(Whoops, again.)

def __init__(self, config_file):
self.fahdata = fahdata.FAHData()
self.INI = ConfigParser.ConfigParser()
if os.path.exists(config_file):
try:
self.INI.read(config_file)
except ConfigParser.Error, err:
print "Cannot parse configuration file. %s" %err
except IOError, err:
print "Problem opening configuration file. %s" %err
except Error, err:
print "Problem with with configuration file. %s" %err

I don't know what Error refers to here. If you want a blanket except
clause then catch Exception, that would replace IOError and probably Error.

Also you can specify more than one exception in a single except clause
by putting them in a tuple:
except (IOError, Error), err:
print "Problem opening configuration file. %s" %err

HTH
Kent
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top