| Author |
can the run() method throw exception
|
ivin jacob
Greenhorn
Joined: Apr 05, 2006
Posts: 23
|
|
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
Joined: Aug 03, 2004
Posts: 244
|
|
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
Joined: Feb 25, 2007
Posts: 1638
|
|
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.
|
apigee, a better way to API!
|
 |
 |
|
|
subject: can the run() method throw exception
|
|
|