How to get a raised exception from other thread

Peter Hansen peter at engcorp.com
Fri Oct 21 12:38:19 CEST 2005


dcrespo wrote:
> Ok, sorry about the above question. I solved it adding this to the main
> thread:
> 
> try:
>     SrvrTCP = module.ThreadedTCPServer(ip,port)
>     SrvrTCP.start()
> except Exception, description:
>     MsgBox(self,"TCPServer
> Error:\n\n"+str(description),title="TCPServer",style=wx.OK |
> wx.ICON_ERROR)
>     return
> 
> Peter, thank you very much.

You're quite welcome.  It's nice to be able to provide a "perfect" 
answer, for a change. :-)

One suggestion about the above: "description" is actually the exception 
instance (the object), not just the description.  The except statement 
can take two items after: the exception type(s) to catch and, 
optionally, a name to bind to the exception instance.  But since 
exception objects know how to represent themselves as strings, calling 
str() on it gives you the description you wanted.  Therefore it would be 
more readable/correct to say this:

except Exception, ex:
     MsgBox(..... str(ex) ... )

But I'm happy it works either way!

-Peter



More information about the Python-list mailing list