Defining one global UncaughtExceptionHandler for all threads of my application

笑着哭i 提交于 2019-11-29 01:45:39

Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler ex)

This should achieve what you are looking for.

As the doc says

Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

And an interesting note (also in the docs) regarding you using the handler in the ThreadGroup

Note that the default uncaught exception handler should not usually defer to the thread's ThreadGroup object, as that could cause infinite recursion.

You need to set the default uncaught exception handler. This is a static method on the Thread class called setDefaultUncaughtExceptionHandler. Doing this will do set the exception handler for the application running. It will be the default for any new threads unless otherwise specified.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!