1

I am searching on net but did not get satisfactory answers that what made Sun engineers to disallow run method of Runnable interface for throwing an exception.

T-Bag
  • 10,916
  • 3
  • 54
  • 118

6 Answers6

3

The intention behind the concept of multi-threading is to allow parallel processing of two or more tasks for which the run method is used. If the run method throws a CheckedException, in that case the calling thread has to wait in the corresponding catch block to wait for the same which defies the sole purpose of multi-threading.

For example: Consider the following HYPOTHETICAL scenario
The main thread starts another thread say Thread1 which supposedly say does throw some CheckedException. Now to catch that exception, the corresponding catch block has to be put somewhere. Lets say the main method itself has the same. Now to catch that exception (generate by the Thread1's run method), the main thread has to wait inside the catch block to let the execution of Thread1's run method complete which will not be acceptable as then there would be no use of multithreading here.

Moreover the Future and Callable tasks are based on the above hypothesis only.

gaurs
  • 575
  • 1
  • 5
  • 19
2

The answer to the question can be a question to ask yourself that if run method throws an exception then what would catch it?

From the JCIP docs:

Runnable is a fairly limiting abstraction; run can not return a value or throw checked exception .

However you can use Callable, and submit it to an ExecutorService and waiting for result with FutureTask.isDone() returned by the ExecutorService.submit()

Something like this:

Callable<Void> callable = new Callable<Void>() {
    public Void call() {
        // whatever you want
        return null;
    }
};
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

To fill that gap since Jdk 1.5 onwords Callable<V> is introduced which provides you create async task and that returns a result and may throw an exception.

Code snippet -

public class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
       ... 
    }

}

...

FutureTask<String> futureTask1 = new FutureTask<String>(new MyCallable());
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(futureTask1);
try{
    String result = futureTask1.get();
    ...
}catch(Exception ex){ // catch that exception
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

The calling method does not call the run() directly to start a Thread. One needs to call Thread.start() which in turn calls a run() method. So there is no point in throwing an checked exception from the run() method.

Also, any exception you throw in run method will be carefully ignored by JVM. Thus, throwing it there is probably a mistake (unless you have specific exception handler for the thread)

Refer this for more explanation: Java Thread: Run method cannot throw checked exception

Community
  • 1
  • 1
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

Checked Exception vs Unchecked Exception.

The first point is a wrong assumption that run() method cannot throw an exception. See the below code compiles

public class Test  extends Thread{

    @Override
    public void run() throws IllegalArgumentException {
        // TODO Auto-generated method stub
        super.run();
    }

}

And now see the below code , which is won't compile.

public class Test  extends Thread{

    @Override
    public void run() throws FileNotFoundException {
        // TODO Auto-generated method stub
        super.run();
    }

}

The point is the run() nethod cannot throws a Checked Exception, If it can throw which Exception we would catch ??

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • If main() allows us to throw exception and JVM handles it then why not JVM handles run() exception. – T-Bag Aug 20 '14 at 06:39
0

if any calling method(for example void m1()) throws exception called method (pp.m1()) has to either solve or rethrow the exception just follows////

        public class Practice14 {

         public void m1() throws Exception{


         System.out.println("hello");

          }

      public static void main(String[] args)  {

    Practice14 pp=new Practice14();

    try {

        pp.m1();//here solving the exception

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }
}

so because start() method in thread doesn't have capability to solve the exception run method won't allow you to throws exception

SureshBonam
  • 196
  • 1
  • 4