• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching exceptions thrown in a thread

 
san sui
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings...
Is there a way for a parent process to
catch an exception thrown by a thread it
spawned ?
Since one cannot override the run method
is there a way at all for the thread
to convey to its parent that it has
thrown an exception ?
Thanks.
 
Neil Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am going out on a limb here with all the experts around.
My experience is that because each thread runs in its own context with its own stack, you cannot throw exceptions up out of a thread. However, you can wrap the entire run method with a try-catch(Exception) and then call a method back in the parent, before dying. Note that this call runs in the context of the thread, not the parent, so you must basically use this method to then 'wake-up' the parent or to set a flag indicating that the thread died.
Hope this helps.
Neil
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at java.lang.ThreadGroup. It has a method uncaughtException() which is called by the JVM when a thread in this ThreadGroup stops because of an uncaught exception.

You can basically subclass ThreadGroup and override this method to handle the uncaught exception.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using both techiques...calling methods to indicate success or failure or relying on uncaughtException() are ways to deal with this. You may be interested in an article I co-authored on this exact subject that is in the August 1998 issue of Java Report. It's online here.
Peter Haggar
 
akmal jah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter
I looked at your article, looks like that's what I wanted, but I cannot get source listing from that site, is there a way I can get example source code of that article ?
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Send me your email address and I'll mail you the code.
Peter Haggar
 
akmal jah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks peter . appreciate your help here
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!