473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 6258
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
1
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
2
by: Erik Funkenbusch | last post by:
Does anyone have any suggestions for how to deal with exceptions in inline DataBinder code? For example, I want to convert a string to a date type, but the string may contain non-date strings in...
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
1
by: Military Smurf | last post by:
Okay, so this borrowed code is for doing a recursive file search. The problem is when I encounter the System Volume Information folder, a special kind of exception is thrown that ends up...
7
by: asdf | last post by:
The code I work on has a class called "CException" which gets thrown whenever there is an exception. Various other exception class derive from this. The ctor for the class CException prints...
5
by: Dave the Funkatron | last post by:
Hey all, I'm using MinGW as part of my toolchain in Eclipse, and I am trying to figure out why I am getting a compiler error when I include the <limitsheader. The command that eclipse is...
2
by: Sarkast | last post by:
Hi there, I have a quick, but apparently quite complicated question. I want my whole site to be unselectable, with a few exceptions. Unfortunatly I am not able to do this. I have a semi...
7
by: fjm | last post by:
Hello everyone, Can someone please give me a hand with an exception? I cannot seem to get this to work correctly or I just don't fully understand exceptions. It's my assumption that I should be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.