Java:全局异常处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Global Exception Handler
提问by Martijn Courteaux
Is there a way to make a global exception-handler in Java. I want to use like this:
有没有办法在 Java 中创建全局异常处理程序。我想这样使用:
"When an exception is thrown somewhere in the WHOLE program, exit."
The handler may not catch exceptions thrown in a try-catch
body.
处理程序可能无法捕获try-catch
主体中抛出的异常。
Martijn
马丁
采纳答案by bobbymcr
Use Thread.setDefaultUncaughtExceptionHandler. See Rod Hilton's "Global Exception Handling" blog post for an example.
使用Thread.setDefaultUncaughtExceptionHandler。有关示例,请参阅 Rod Hilton 的“全局异常处理”博客文章。
回答by Kolibri
You can set the default UncaughtExceptionHandler , which will be used whenever a exception propegates uncaught throughout the system.
您可以设置默认的 UncaughtExceptionHandler,只要在整个系统中传播未捕获的异常,就会使用它。
回答by hypercube
DefaultUncaughtExceptionHandler is the correct answer. It was revealed to me by Jeff Storeyat thislocation, a few days ago. As u suspected, the "manually" caught exceptions will never be caught by this handler. However i got the following warning :
DefaultUncaughtExceptionHandler 是正确的答案。几天前,Jeff Storey在这个位置向我透露了这一点。正如你所怀疑的,“手动”捕获的异常永远不会被这个处理程序捕获。但是我收到以下警告:
**- To be compliant to J2EE, a webapp should not use any thread.**
**- 为了符合 J2EE,Web 应用程序不应使用任何线程。**
when i have checked my project against good-practice and recommended java coding style with PMDplug-in for EclipseIDE.
回答by Clifford Oravec
For clarification, use setDefaultUncaughtExceptionHandler for standalone Java applications or for instances where you are sure you have a well-defined entry point for the Thread.
为澄清起见,请将 setDefaultUncaughtExceptionHandler 用于独立的 Java 应用程序,或者用于您确定具有明确定义的线程入口点的实例。
For instances where you do not have a well-defined entry point for the Thread, for example, when you are running in a web server or app server context or other framework where the setup and teardown are handled outside of your code, look to see how that framework handles global exceptions. Typically, these frameworks have their own established global exception handlers that you become a participant in, rather than define.
对于没有明确定义的线程入口点的实例,例如,当您在 Web 服务器或应用程序服务器上下文或其他框架中运行时,在代码之外处理设置和拆卸,请查看该框架如何处理全局异常。通常,这些框架都有自己已建立的全局异常处理程序,您可以参与其中,而不是定义这些处理程序。
For a more elaborate discussion, please see http://metatations.com/2011/11/20/global-exception-handling-in-java/
有关更详细的讨论,请参阅http://metatations.com/2011/11/20/global-exception-handling-in-java/
回答by Brad Mace
Here's an example which uses Logbackto handle any uncaught exceptions:
这是一个使用Logback处理任何未捕获异常的示例:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
LoggerFactory.getLogger("CustomLogger").error("Uncaught Exception in thread '" + t.getName() + "'", e);
System.exit(1);
}
});
This can also be done on a per-thread basis using Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)
这也可以在每个线程的基础上使用 Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)
回答by Jeff Learman
Threads.setDefaultUncaughtExceptionHandler() works but not in all cases. For example, I'm using it in my main() before creating Swing widgets, and it works in the threads created by Swing, such as the AWT event thread or SwingWorker threads.
Threads.setDefaultUncaughtExceptionHandler() 有效,但并非在所有情况下。例如,我在创建 Swing 小部件之前在我的 main() 中使用它,并且它在 Swing 创建的线程中工作,例如 AWT 事件线程或 SwingWorker 线程。
Sadly, it doesn't have any effect on the thread created by javax.naming.spi.NamingManager.getInitialContext() when using an LDAP URL, using JavaSE 1.6. No doubt there are other exceptions.
遗憾的是,在使用 JavaSE 1.6 的 LDAP URL 时,它对 javax.naming.spi.NamingManager.getInitialContext() 创建的线程没有任何影响。毫无疑问,还有其他例外。