我们希望在应用程序日志中跟踪这些异常-默认情况下,Java只会将它们输出到控制台。
从Java 7开始,您必须以不同的方式进行操作,因为sun.awt.exception.handler hack不再起作用。
这是解决方案(来自Java 7中的Uncaught AWT Exceptions)。
1 2 3 4 5 6 7 8 9 10 11 12
   | // Regular Exception 
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler()); 
 
// EDT Exception 
SwingUtilities.invokeAndWait(new Runnable() 
{ 
    public void run() 
    { 
        // We are in the event dispatching thread 
        Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler()); 
    } 
});  | 
 
 
在EDT中和EDT外,未捕获的异常之间有区别。
另一个问题有一个解决方案,但是如果您只想咀嚼EDT部分...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | class AWTExceptionHandler { 
 
  public void handle(Throwable t) { 
    try { 
      // insert your exception handling code here 
      // or do nothing to make it go away 
    } catch (Throwable t) { 
      // don't let the exception get thrown out, will cause infinite looping! 
    } 
  } 
 
  public static void registerExceptionHandler() { 
    System.setProperty('sun.awt.exception.handler', AWTExceptionHandler.class.getName()) 
  } 
}  | 
 
对shemnons的补充:
EDT中第一次发生未捕获的RuntimeException(或Error)时,它正在寻找属性" sun.awt.exception.handler",并尝试加载与该属性关联的类。 EDT需要Handler类具有默认的构造函数,否则EDT将不使用它。
如果您需要为处理过程带来更多动态,则必须使用静态操作来执行此操作,因为该类是由EDT实例化的,因此没有机会访问静态以外的其他资源。这是我们正在使用的Swing框架中的异常处理程序代码。它是为Java 1.4编写的,在这里工作得很好:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
   | public class AwtExceptionHandler { 
 
    private static final Logger LOGGER = LoggerFactory.getLogger(AwtExceptionHandler.class); 
 
    private static List exceptionHandlerList = new LinkedList(); 
 
    /** 
     * WARNING: Don't change the signature of this method! 
     */ 
    public void handle(Throwable throwable) { 
        if (exceptionHandlerList.isEmpty()) { 
            LOGGER.error("Uncatched Throwable detected", throwable); 
        } else { 
            delegate(new ExceptionEvent(throwable)); 
        } 
    } 
 
    private void delegate(ExceptionEvent event) { 
        for (Iterator handlerIterator = exceptionHandlerList.iterator(); handlerIterator.hasNext();) { 
            IExceptionHandler handler = (IExceptionHandler) handlerIterator.next(); 
 
            try { 
                handler.handleException(event); 
                if (event.isConsumed()) { 
                    break; 
                } 
            } catch (Throwable e) { 
                LOGGER.error("Error while running exception handler:" + handler, e); 
            } 
        } 
    } 
 
    public static void addErrorHandler(IExceptionHandler exceptionHandler) { 
        exceptionHandlerList.add(exceptionHandler); 
    } 
 
    public static void removeErrorHandler(IExceptionHandler exceptionHandler) { 
        exceptionHandlerList.remove(exceptionHandler); 
    } 
 
}  | 
 
 
希望能帮助到你。
有两种方法:
/ *在EDT上安装Thread.UncaughtExceptionHandler * /
设置系统属性:
System.setProperty(" sun.awt.exception.handler",MyExceptionHandler.class.getName());
我不知道后者是否适用于非SUN的jvm。
-
 
确实,第一种方法是不正确的,它只是一种检测崩溃线程的机制。