Why the exception is not caught in this multithread example?

Viewed 131

I was trying to learn how exception propagate in multithread.

Question

In the following example. Why is the RuntimeException exception not caught in

`System.out.println("Exception handled " + e); 

Given Code

package p05_Interrupt;

public class D05_Interrupt1 extends Thread {
  public void run()  {
    try {
      Thread.sleep(1000);
      System.out.println("task");
    } catch (InterruptedException e) {
      throw new RuntimeException("Thread interrupted..." + e);
    }
  }

  public static void main(String args[]) {
    D05_Interrupt1 t1 = new D05_Interrupt1();
    t1.start();
    try {
      t1.interrupt();
    } catch (Exception e) {
      System.out.println("Exception handled " + e); // Why not print this line 
    }
  }
}

1st example in JavaTpoint "Example of interrupting a thread that stops working"

Output

Exception in thread "Thread-0" java.lang.RuntimeException: Thread interrupted...java.lang.InterruptedException: sleep interrupted
    at p05_Interrupt.D05_Interrupt1.run(D05_Interrupt1.java:9)

Is that because this is multithreading, so exception is printed in the thread t1, and the output of main() thread is hidden?

I also tried to add throws at public void run() throws RuntimeException { didn't change anything.

1 Answers

You cannot catch (like that) the exception thrown by the Thread in the main thread. However, you can implement Thread.UncaughtExceptionHandler to achieve that.

public static interface Thread.UncaughtExceptionHandler Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception.

When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.

public class D05_Interrupt1 extends Thread 
        implements Thread.UncaughtExceptionHandler {

    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("task");
        } catch (InterruptedException e) {
            throw new RuntimeException("Thread interrupted..." + e);
        }
    }

    public static void main(String args[]) {
        D05_Interrupt1 t1 = new D05_Interrupt1();
        Thread.setDefaultUncaughtExceptionHandler(t1);
        t1.start();
        t1.interrupt();
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("Exception handled " + e); // Why not print this line
    }
}

Output:

Exception handled java.lang.RuntimeException: Thread interrupted...java.lang.InterruptedException: sleep interrupted
Related