This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exceptions from run method  RSS feed

 
Maalti Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How can we hanlde exceptions from run method. I want the run method throw an exception and calling thread handles the exception.

Thanks for your time.
Maalti Iyer
 
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
The run() method is the top level method in its thread; the Thread class itself invokes run() and if run() throws an exception, Thread prints a stack trace. You refer to the "caller", but think about it: the "caller" you're talking about calls start(), then moves on; it's not waiting around for your run() method to throw an exception.
What you want, really, is to be able to report errors from a running thread, and that's fine. There are lots of ways to do this: run() can call an error-reporting method of your own design; or a Runnable object can include a member variable to serve as an error flag, and run() can set it and the caller can check it.
You could definitely benefit from reading Doug Lea's classic Concurrent Programming in Java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!