如果某个方法从EDT线程调用未经检查的异常,会发生什么?它是否会阻止GUI的响应性或什么?谢谢
答案 0 :(得分:5)
在重新启动之前,EDT是否会关闭之前显示的所有组件?
不,EDT只是像以前一样恢复执行Runnable
个实例;破碎的Runnable
继续抛出异常。作为练习,请检查调试器中的example cited。
附录:这是此example的典型堆栈跟踪。
chart.DTSCTest$1.actionPerformed(DTSCTest.java:53) javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028) javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351) javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) java.awt.Component.processMouseEvent(Component.java:6373) javax.swing.JComponent.processMouseEvent(JComponent.java:3267) java.awt.Component.processEvent(Component.java:6138) java.awt.Container.processEvent(Container.java:2085) java.awt.Component.dispatchEventImpl(Component.java:4735) java.awt.Container.dispatchEventImpl(Container.java:2143) java.awt.Component.dispatchEvent(Component.java:4565) java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621) java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282) java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212) java.awt.Container.dispatchEventImpl(Container.java:2129) java.awt.Window.dispatchEventImpl(Window.java:2478) java.awt.Component.dispatchEvent(Component.java:4565) java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679) java.awt.EventQueue.access$000(EventQueue.java:85) java.awt.EventQueue$1.run(EventQueue.java:638) java.awt.EventQueue$1.run(EventQueue.java:636) java.security.AccessController.doPrivileged(AccessController.java) java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) java.awt.EventQueue$2.run(EventQueue.java:652) java.awt.EventQueue$2.run(EventQueue.java:650) java.security.AccessController.doPrivileged(AccessController.java) java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) java.awt.EventQueue.dispatchEvent(EventQueue.java:649) java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 1 :(得分:4)
默认情况下,如果未捕获异常,则会将堆栈跟踪写入控制台输出。整个GUI不会变得没有响应(但是,正如垃圾桶评论,特定组件可能保持不自然状态),EDT继续工作:Does the EDT restart or not when an exception is thrown?
好的做法是设置一个未捕获的异常处理程序,因为你想知道是否出了什么问题。请注意(取决于Java版本),对于EDT而言,这可能与其他线程的功能不同:
How can I detect when an Exception's been thrown globally in Java?
请注意" sun.awt.exception.handler"许多SO帖子中提到的技巧不是必需的,在Java 7中不起作用。对于Java 7,只需使用标准的Thread.setDefaultUncaughtExceptionHandler。当然,如果您使用这两种机制来注册异常处理程序,则代码将适用于所有版本。