File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Catching exception from a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Catching exception from a Thread" Watch "Catching exception from a Thread" New topic

Catching exception from a Thread

Sudhir Shankar

Joined: Jun 01, 2006
Posts: 4
I have a problem with thread. I have written a thread class implementing runnable interface. In run method I am getting exception which need to be propagated back to the method that started the thread. I am not able to do this as I cant declare throws to run() method. Also throwing RuntimeException is not getting the exception to propogate back to the calling method.

Pls help

Henry Wong

Joined: Sep 28, 2004
Posts: 20531

I am not able to do this as I cant declare throws to run() method. Also throwing RuntimeException is not getting the exception to propogate back to the calling method.

Think about it. This code is running in another thread. The main thread is off doing something else -- probably not even directly related anymore. Or to put it another way, the "calling method" may have finished and returned, a long time ago.

Anyway, how did you implement getting the results sent back to the original thread? I am assuming that you stored it in a common location (which you synchronized. etc.). You just need to enhance that technique a bit.

Instead of storing just the result, you now need to store some flag whether an exception occurred, and of course, the exception too. In you run() method, you need to catch the exception so that you may store it.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Hi Sudhir,

As mentioned by Henry Wong, the caller method will start the thread and will continue (and may finish off) without waiting for result (or exception).

Hence, if your requirement is to catch the exception thrown in the thread in your caller method, the first thing you should be prepared for is having a synchronous (blocking) call from the caller method to the thread.

But then, multi-threading won't make much of a sense, would it? Your parent thread will simply 'hang' till the time child thread finishes off (or throws an exception). Although such a case sometimes becomes relevant (particularly in a Swing application).

Does your application have a GUI? If you could tell more about your requirements, may be folks can help you more.


- Aditya Jha
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
FutureTask (new in 1.5) is built just for this kind of thing. It can capture a result or an exception from another thread.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Think of the thread that is run as a job. The thread that started it must periodically check to see the status of the job.

The job thread should wrap its run method in a try-catch block that catches Throwable and stores it before shutting itself down. The main thread can then request this exception from the job thread when it gets around to it.
I agree. Here's the link:
subject: Catching exception from a Thread
It's not a secret anymore!