aspose file tools
The moose likes Threads and Synchronization and the fly likes Catching exceptions thrown in a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Catching exceptions thrown in a thread" Watch "Catching exceptions thrown in a thread" New topic
Author

Catching exceptions thrown in a thread

san sui
Greenhorn

Joined: Feb 25, 2002
Posts: 3
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
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
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
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


Senior Software Engineer, IBM
author of: Practical Java
akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
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
Send me your email address and I'll mail you the code.
Peter Haggar
akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
thanks peter . appreciate your help here
 
 
subject: Catching exceptions thrown in a thread
 
Threads others viewed
exception handling in threads
Death of a thread
try catch block
Does setPriority stop thread..?
How to throw exception in run method
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture