Little Help with Exceptions and ConfigParser

mwt michaeltaft at gmail.com
Tue Mar 14 13:47:04 EST 2006


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




More information about the Python-list mailing list