473,757 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Co nfigParser()
if os.path.exists( config_file):
try:
self.INI.read(c onfig_file)
except ConfigParser.Pa rsingError:
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 6302
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.Co nfigParser()
if os.path.exists( config_file):
try:
self.INI.read(c onfig_file)
except ConfigParser.Pa rsingError:
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.Er ror so
if you catch that instead of ConfigParser.Pa rsingError 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.Co nfigParser()
if os.path.exists( config_file):
try:
self.INI.read(c onfig_file)
except ConfigParser.Er ror, 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.Co nfigParser()
if os.path.exists( config_file):
try:
self.INI.read(c onfig_file)
except ConfigParser.Er ror, 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.Co nfigParser()
if os.path.exists( config_file):
try:
self.INI.read(c onfig_file)
except ConfigParser.Er ror, 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
1340
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 ? Try it out.
1
1137
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 ? Try it out.
2
1008
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 rare occasions. This will throw an invalid cast exception. Any suggestions?
3
4594
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 { public:
1
995
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 terminating the subroutine. I have two other exceptions that are thrown before the one I described does, yet they don't terminate the subroutine. Can anyone tell me why one kind of exception terminates this subroutine, while another does not?
7
1728
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 out the stack trace using a system function. So in the code where there is a "throw CException()" the stack trace gets printed. Even if a derived class gets throwen the stack trace gets printed because a CException object is created. Question I...
5
2481
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 running is g++ -IC:\Dave\School\common\cxx -IC:\Dave\School\common\third party \packages\glut-3.7.6-bin\include -IC:\Dave\School\common\third party
2
2970
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 working versions, with onselectstart="return false" in the body tag, but then I can't select anything. I also have a version with a function which iterates over the whole HTML and makes everything with "unselectable" in the class name unselectable by:...
7
1350
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 able to trap an exception, log that exception and have the code continue, not halt. What happens is that my code just stops dead instead of continuing. I don't have a subclass to the exception class and maybe that is the problem. Can someone...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.