Little Help with Exceptions and ConfigParser

Discussion in 'Python' started by mwt, Mar 14, 2006.

  1. mwt

    mwt Guest

    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
    mwt, Mar 14, 2006
    #1
    1. Advertising

  2. mwt

    mwt Guest

    Whoops. Those page numbers are from "Cookbook."
    As well as the Except section in "Nutshell."
    Still stewing away here. ;)
    mwt, Mar 15, 2006
    #2
    1. Advertising

  3. mwt

    Kent Johnson Guest

    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
    Kent Johnson, Mar 15, 2006
    #3
  4. mwt

    mwt Guest

    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
    mwt, Mar 15, 2006
    #4
  5. mwt

    mwt Guest

    (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
    mwt, Mar 15, 2006
    #5
  6. mwt

    Kent Johnson Guest

    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
    Kent Johnson, Mar 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Boulet

    Help with ConfigParser

    Stephen Boulet, May 2, 2004, in forum: Python
    Replies:
    3
    Views:
    396
    Peter Otten
    May 2, 2004
  2. Dan Gass
    Replies:
    0
    Views:
    455
    Dan Gass
    Sep 13, 2004
  3. ThaDoctor
    Replies:
    3
    Views:
    381
    Alan Woodland
    Sep 28, 2007
  4. Replies:
    3
    Views:
    428
  5. Daniel
    Replies:
    1
    Views:
    210
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page