Thread Won'T Propagate Exception
public class ThreadHiddenThrow {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {throw new RuntimeException();});
try {
thread.start();
thread.join();
} catch (Exception e) {
System.out.println("Doesn't catch exception at all ");
}
Thread.sleep(2000);
System.out.println("Still run even though exception has thrown");
}
}
Exception happens in a thread will stay in that thread.
Exception handler
We can set a specific exception handler, eventhough parent will not be propagate
public class ThreadHiddenThrow {
public static void main(String[] args) throws InterruptedException {
// To handle it
Thread handleExceptionThread = new Thread(() -> {throw new RuntimeException();});
handleExceptionThread.setUncaughtExceptionHandler((t, exception) -> {
System.out.println("Uncaught exception: " + exception + " from thread: " + t);
});
try {
handleExceptionThread.start();
handleExceptionThread.join();
} catch (Exception e) {
System.out.println("Doesn't catch exception at all ");
}
Thread.sleep(2000);
}