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
Joined: Jan 22, 2002
Posts: 11
posted
0
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
Joined: Jun 27, 2000
Posts: 213
posted
0
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
Joined: Jan 03, 2001
Posts: 106
posted
0
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
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
Joined: Jan 03, 2001
Posts: 106
posted
0
Send me your email address and I'll mail you the code. Peter Haggar