如何查找Swing组件抛出的未知异常的(信息性)堆栈跟踪

时间:2015-07-20 18:50:44

标签: java swing uncaughtexceptionhandler

我在Swing应用程序中设置了UncaughtExceptionHandler,它将捕获并记录我的代码中的任何未捕获的异常并向我报告。大多数这些错误的堆栈跟踪有助于诊断问题。但是,有时我会遇到这样的问题:

Thread : AWT-EventQueue-2
   Thread[AWT-EventQueue-2,6,javawsApplicationThreadGroup]

Error Message:
java.lang.Double cannot be cast to java.lang.Integer

Error String:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer

StackTrace: class java.lang.ClassCastException
   java.lang.Integer.compareTo(Unknown Source)
   javax.swing.table.TableRowSorter$ComparableComparator.compare(Unknown Source)
   javax.swing.DefaultRowSorter.compare(Unknown Source)
   javax.swing.DefaultRowSorter.access$100(Unknown Source)
   javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
   javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
   java.util.ComparableTimSort.binarySort(Unknown Source)
   java.util.ComparableTimSort.sort(Unknown Source)
   java.util.Arrays.sort(Unknown Source)
   javax.swing.DefaultRowSorter.sortExistingData(Unknown Source)
   javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
   javax.swing.DefaultRowSorter.toggleSortOrder(Unknown Source)
   javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(Unknown Source)
   java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
   java.awt.Component.processMouseEvent(Unknown Source)
   javax.swing.JComponent.processMouseEvent(Unknown Source)
   java.awt.Component.processEvent(Unknown Source)
   java.awt.Container.processEvent(Unknown Source)
   java.awt.Component.dispatchEventImpl(Unknown Source)
   java.awt.Container.dispatchEventImpl(Unknown Source)
   java.awt.Component.dispatchEvent(Unknown Source)
   java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   java.awt.Container.dispatchEventImpl(Unknown Source)
   java.awt.Window.dispatchEventImpl(Unknown Source)
   java.awt.Component.dispatchEvent(Unknown Source)
   java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   java.awt.EventQueue.access$500(Unknown Source)
   java.awt.EventQueue$3.run(Unknown Source)
   java.awt.EventQueue$3.run(Unknown Source)
   java.security.AccessController.doPrivileged(Native Method)
   java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
   java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
   java.awt.EventQueue$4.run(Unknown Source)
   java.awt.EventQueue$4.run(Unknown Source)
   java.security.AccessController.doPrivileged(Native Method)
   java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
   java.awt.EventQueue.dispatchEvent(Unknown Source)
   java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   java.awt.EventDispatchThread.run(Unknown Source)

我不需要帮助来诊断此特定问题,问题很明显(用户正在尝试在应用程序的某处对JTable进行排序,但该列中的值为Double s,但表宣称期望Integer s)。但是,我不知道这个堆栈跟踪JTable是问题的根源(我的应用程序中有很多表)。在不知道问题来自哪里,我无法修复它。

那么,我是否可以获得更多有用的调试信息来帮助找到此问题的来源以及其他类似的问题?

注意:不仅仅是每一行都说明了#34;未知来源,"因为堆栈跟踪中列出的类都不是我的类,所以它们都是库类。因此,即使他们给问题提供了一个行号,也就是我无法修复的课程。

1 个答案:

答案 0 :(得分:1)

在这种情况下,我重写一些东西只是为了添加调试信息。例如,在这种情况下,我们在堆栈跟踪中看到调用了DefaultRowSorter.setSortKeys,因此我们可以使用类

public class MyRowSorter<M, I> extends DefaultRowSorter<M, I> {
    ....

    @Override
    public void setSortKeys(List<? extends SortKey> sortKeys) {
        try {
            super.setSortKeys(sortKeys);
        } catch (Exception e) {
            //   
            // print debugging info here!
            //
            throw e;
        }
    }
}

现在您可以设置MyRowSorter对表进行排序,将必要的调试信息添加到MyRowSorter,然后您可以调试哪个表有问题。