Java – defines a global uncaughtexceptionhandler for all threads of the application

I want to define an application level uncaughtexceptionhandler in my java application. If an uncaught exception is thrown in a thread of my application, it will be called

So for example, I want something like this:

1° LEVEL ---> Call thread own UncaughtExceptionHandler ---> 2° LEVEL Call Thread Group UncaughtExceptionHandler ---> 3° LEVEL Call application(default) UncaughtExceptionHandler

Simply put, I want to override the default uncaughtexceptionhandler and define my own handler, not in system Print stack trace on err (this is the default behavior)

For example, in C # Net, I will execute the unhandled and thread exception event handling functions in the main () method of the processing application:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Can it even be done in Java?

How do I override the default uncaughtexceptionhandler in Java?

Solution

Thread. setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler ex)

This should achieve what you are looking for

As the document says

And an interesting comment (also in the documentation) about your use of handlers in ThreadGroup

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>