Little Help with Exceptions and ConfigParser
Kent Johnson
kent at kentsjohnson.com
Tue Mar 14 22:22:45 EST 2006
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
More information about the Python-list
mailing list