how to use bool

Scott David Daniels Scott.Daniels at Acm.Org
Sun Jan 6 13:52:35 EST 2008


jimgardener at gmail.com wrote:
> ...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
> 

Almost.  The callingmethode code above behaves like:
     def callingmethode(self):
         mymethod()
except that your code hides the traceback information.
If you need to do something locally (other than passing
along the exception), use either:
     def callingmethode(self):
         try:
            mymethod()
         finally:
            cleanup_stuff()
or (if you have some exception-specific code to do):
     def callingmethode(self):
         try:
            mymethod()
         except MyError,myerr:
            stuff_only_for_exceptional_cases()
            raise  # Note that this keeps the original traceback.


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list