aspose file tools
The moose likes Threads and Synchronization and the fly likes can the run() method throw exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "can the run() method throw exception" Watch "can the run() method throw exception" New topic
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: can the run() method throw exception
 
Similar Threads
Thread doubt
How to capture the exceptions thrown from third party services in servlet
Exception Handling..awesome
Why we cannot call getRollbackOnly() within a method marked Support?
exceptions from run method