470,811 Members | 1,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Little Help with Exceptions and ConfigParser

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

Mar 14 '06 #1
5 6008
mwt
Whoops. Those page numbers are from "Cookbook."
As well as the Except section in "Nutshell."
Still stewing away here. ;)

Mar 15 '06 #2
mwt wrote:
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
Mar 15 '06 #3
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

Mar 15 '06 #4
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

Mar 15 '06 #5
mwt wrote:
(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
Mar 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Erik Funkenbusch | last post: by
3 posts views Thread by Dmitry Prokoptsev | last post: by
1 post views Thread by Military Smurf | last post: by
5 posts views Thread by Dave the Funkatron | last post: by
7 posts views Thread by fjm | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.