Exception propagation to the parent thread

Hello,

How can I cause an exception in the parent thread if the error occurs in the child thread?

I use the Callable interface, have a try catch block in its run() method and want when an exception appears and is caught by this block to also generate an exception in the parent thread so it can be caught there, in the parent's try catch block.

Hope I'm making sense here :)

Thank you.
acilonvAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OnityCommented:
0
moosely123Commented:
One way is to have the child thread's methods to throw a new exception.

You can use a try/catch block to choose which ones to propogate like this

try {
 //do something
}
catch (SomeException e) {
    throw new SomeException();
}

of course you can just catch Exception to propagate all exceptions
0
wannabetechieCommented:
This is one way of handling exceptions with callable interface implementations

public class MyCallable implements Callable
{
  public Integer call () throws java.io.IOException {
    return 1;
  }
}

As the callable interface has to be called with an ExecutorService you can do something like this

FutureTask task = new FutureTask (new MyCallable ());
ExecutorService es = Executors.newSingleThreadExecutor ();
es.submit (task);
try {
  int result = task.get ();
  System.out.println ("Result from task.get () = " + result);
}
catch (Exception e) {
  System.err.println (e);
}
es.shutdown ();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
acilonvAuthor Commented:
I found the cause of my problem. The Exception was not passed because I did not use the get() method of the FutureTask problem. Only when this method is called the error thrown in the thread gets picked up by the parent thread.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.