Why is UncaughtExceptionHandler not called by ExecutorService?

前端 未结 6 2085
你的背包
你的背包 2020-12-23 09:01

I\'ve stumbled upon a problem, that can be summarized as follows:

When I create the thread manually (i.e. by instantiating java.lang.Thread) the U

6条回答
  •  遥遥无期
    2020-12-23 10:06

    There is a little bit of a workaround. In your run method, you can catch every exception, and later on do something like this (ex: in a finally block)

    Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), ex);
    //or, same effect:
    Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), ex);
    

    This will "ensure a firing" of the current exception as thrown to your uncoughtExceptionHandler (or to the defualt uncought exception handler). You can always rethrow catched exceptions for pool worker.

提交回复
热议问题