I borrowed this from another question:
@Slf4j
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread() {
public void run() {
try {
//do some stuff
LOG.debug("about to throw a RE from a worker thead");
} catch (Exception e) {
e.printStackTrace();
}
throw new RuntimeException("purposeful!");
}
};
LOG.debug("in main thead");
t.setUncaughtExceptionHandler(
(thread, throwable) -> LOG.debug("in main thread; catching exception from worker. uncaught exception from worker thread: {} from {}",
throwable.getMessage(), thread.getName()));
t.start();
TimeUnit.SECONDS.sleep(10);
LOG.debug("done-main thead");
}
}
If it runs, the following output is produced:
10:19:50.249 [main] DEBUG com.foo.services.search.Main - in main thead
10:19:50.258 [Thread-0] DEBUG com.foo.services.search.Main - about to throw a RE from a worker thead
10:19:50.258 [Thread-0] DEBUG com.foo.services.search.Main - in main thread; catching exception from worker. uncaught exception from worker thread: purposeful! from Thread-0
10:20:00.258 [main] DEBUG com.foo.services.search.Main - done-main thead
Why, when thread-0 is finished, does the throwable-catching-activity appear to be occurring inside this completed thread?