I have this code here. I was trying to get information about unchecked exceptions in Runnable
s of a scheduler without using try catch or the ScheduledFuture
to check for exceptions. I thought this would be the easiest way but it doesn't show any exception. Why isn't there any output visible?
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println("Exception in thread "+t.getName()+":");
e.printStackTrace();
}
};
ThreadFactory tf = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler(handler);
return t;
}
};
ScheduledExecutorService ses = new ScheduledThreadPoolExecutor(1, tf);
ses.schedule(()->Objects.requireNonNull(null), 100, TimeUnit.MILLISECONDS);