0

I have this code here. I was trying to get information about unchecked exceptions in Runnables 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);
Abra
  • 19,142
  • 7
  • 29
  • 41
CoderBendl
  • 11
  • 1
  • You might want to look at other questions like https://stackoverflow.com/questions/3875739/exception-handling-in-threadpools – Progman May 31 '23 at 09:52
  • Why do you not check the returned `ScheduledFuture` instance to see, if the task executed successfully? – Progman May 31 '23 at 10:00
  • Does this answer your question? [SchduledExecutorService not executing UncaughtExceptionHandler when exception happens in the thread](https://stackoverflow.com/questions/58318817/schduledexecutorservice-not-executing-uncaughtexceptionhandler-when-exception-ha) – Progman May 31 '23 at 10:42

0 Answers0