• Post Reply Bookmark Topic Watch Topic
  • New Topic

throwing exception from a thread  RSS feed

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two threads t1 and t2.t1 calls t2,now some exception comes when t2 is executing,so how can it tell that exception to t1.Throws cannot be used in run method.
Please tell me how we can do this.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't, not directly. But there are lots of things you could do. For example, the class that implements t2's Runnable could have a getException() method and an 'exception' member variable. If an exception occurs, t2 can store it in that variable. Then before t1 assumes that t2 completed successfully, it can check to see if getException() returns non-null.

Another possibility would be for t2 to hold a reference to the instance of the class that implements t1, and if there is an exception, t2 can call a method you've provided for this purpose to notify t1. A method like that is called a "callback."
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for your answer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!