Java: Handling exceptions in child threads

后端 未结 3 1494
庸人自扰
庸人自扰 2020-12-06 06:01

I prefer to have the exception handling logic further up in the call stack, near the main method. I like this approach... However, I created a thread where some of its metho

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 06:36

    With Thread.setUncaughtExceptionHandler() you can set an Thread.UncaughtExceptionHandler in your Thread and have a centralized logic of handling exceptions of your threads.

    Moreover you can write your own ThreadFactory that will create you threads with preset Thread.UncaughtExceptionHandler.

提交回复
热议问题