我在tomcat的StartUp上实现了一个UncaughtExceptionHandler:
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOGGER.error("Uncaught Exception");
}
});
当我在 Servlet 中产生异常时,我的处理程序不会捕获它:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
int i = 1/0;
控制台说:
2014 年 2 月 13 日上午 8:23:58 org.apache.catalina.core.StandardWrapperValve 调用 Schwerwiegend:Servlet.service() 用于路径 [/infraview] 上下文中的 servlet [ConnectGatewaysServlet] 引发异常 java.lang.ArithmeticException: / by零在 net.test.gateway.ConnectGatewaysServlet.doPost(ConnectGatewaysServlet.java:73) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java: 728)
如何为 Servlet 实现 UncaughtExceptionHandler?