0

How to propagate the exception to the calling thread in java. Like I have a main thread and I am creating another thread and say new created thread throws an exception. I want to propagate that exception to main thread. How to achieve that.

Dinesh
  • 1,046
  • 1
  • 8
  • 17
  • mmm... catch the exception in the thread and re-throw? – Shreyas Sep 27 '18 at 19:14
  • @Shreyas What's the point of that? – shmosel Sep 27 '18 at 19:15
  • no point...true. Also if not handled in the thread, it would anyways propagate to the main thread. But the question mentioned 'i want to propagate' explicitly. So I guessed the user may want to throw some user defined exception to the main thread. – Shreyas Sep 27 '18 at 19:17
  • 1
    Exceptions are synchronous. Things that happen in other threads are not synchronous. One approach would be to have thread A "return" something like a `Callable` object to thread B, and have the `get()` method raise the exception in thread B at the point where thread B asks for the value. – Ohm's Lawman Sep 27 '18 at 20:03
  • Thanks, I have used the same solution – Dinesh Sep 28 '18 at 05:18

0 Answers0