Little Help with Exceptions and ConfigParser

Kent Johnson kent at kentsjohnson.com
Wed Mar 15 07:54:06 EST 2006


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



More information about the Python-list mailing list