On Jan 6, 8:56 am, [EMAIL PROTECTED] wrote: > some more doubts in this area,,forgive the ignorance of a beginner > > i have > > class MyError(Exception): > def __init__(self,msg) > self.msg=msg > > now my method that can raise this is > > class SomeClass: > ........... > def mymethod(self): > if (somecondition): > raise MyError("somecondn failed") > > if another method in the same class calls this method but wants to > pass the error to a gui code which calls it,,can i do like this > > def callingmethode(self): > try: > mymethod() > except MyError,myerr: > raise myerr > > so that I can handle the error in a gui code that calls > callingmethode() > > class MyGUI: > def guimethode(self): > someinst=SomeClass() > try: > someinst.callingmethode() > except MyError,myer: > self.dealwithMyError(myer) > > is this kind of raising exception the correct way?I am getting syntax > error at > except MyError,myerr: > raise myerr
It's unnecessary to catch the exception and raise it again. The default action for an uncaught exception it to raise it higher. Also this class doesn't make much sense: > class MyError(Exception): > def __init__(self,msg) > self.msg=msg This doesn't call the super class's init function. You'd want to do something like this: > class MyError(Exception): > def __init__(self,msg) > self.msg=msg > Exception.___init__(self) Also, the first argument to the Exception constructor is a text message, so you could just do this: > class MyError(Exception): > def __init__(self,msg) > Exception.___init__(self, msg) This is exactly the default constructor, so you could write it this way: > class MyError(Exception): > pass or as a one-liner: > class MyError(Exception): pass When you want to access the message out of the exception, you can use 'e.message' or 'str(e)'. If you want to print the exception's message, just do 'print e'. Besides that, your code looks good. Might want to read the exceptions chapter of the documentation: http://docs.python.org/tut/node10.html --Bukzor -- http://mail.python.org/mailman/listinfo/python-list