• Post Reply Bookmark Topic Watch Topic
  • New Topic

can the run() method throw exception

 
ivin jacob
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we proceed if we want to throw userdefined exceptions from the run method.

if we write a method called

public void run() throws UserDefinedException{


}


will this get invoked when the thread.start(); method is called


If not how can we handle this

Regards,

ivin

please respond.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not compile.

Thread.run() and Runnable.run() is defined without "throws". So it can't be overriden by a method which throws any checked exceptions.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using java 5 then you can use callable
If not, then write an abstract class that exposes a new abstract method that implementations must override to do the *required* job. (This will contain code that otherwise would have been in run())This class implements Runnable and implements the run() method. In the run method call the abstract method. If it throws an exception then store it and give a getter for this exception. You also have to provide a method to enquire whether the task has finished or not!
Hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!