catching exceptions

jm.suresh@no.spam.gmail.com jm.suresh at gmail.com
Sat Dec 16 06:54:52 EST 2006


Hi, In the following program, I have a class Test which has a property
x. Its setx function gets a string value and converts it into a float
and stores into it.

class Test(object):
    def _getx(self):
        return self._x
    def _setx(self,strvalue):
        try:
            self._x = float(strvalue)
        except ValueError:
            print 'Warning : could not set x attribute to %s' %
strvalue
    x = property(_getx,_setx)


def func1(value):
    a = Test()
    try:
        a.x = value
    except ValueError:
        #Pop up a error window.
        print 'This is second catch'


func1('12')
func1('12e1')
func1('a')



func1('12')
func1('12e1')
func1('a')
>>> func1('a')
Warning : could not set x attribute to a

I am looking for a way to call func1's exception handler also.
Basically I want to have the class's error handler as the basic text
based one, and in the calling side, If I have gui, I will pop-up a
window and say the error message.
One solution is to remove the exception handling inside the class and
leave the entire thing to the caller (software people call this
client?)  side -- if the caller has access to gui it will use gui or
else will print the message. Any other way?

--
Suresh




More information about the Python-list mailing list