2

What would happen if certain method invoked from EDT thread thew unchecked exception? Does it hold up responsiveness of GUI or what? Thank you

user1329572
  • 6,176
  • 5
  • 29
  • 39
MinhHoang
  • 681
  • 3
  • 9
  • 22

2 Answers2

5

Before restarting, does the EDT shutdown all components that were previously displayed?

No, the EDT just resumes executing Runnable instances as it did before; broken Runnables continue throwing exceptions. As an exercise, examine the example cited in the debugger.

Addendum: Here's a typical stack trace from this 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)
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • did you mean that code inside method actionPerformed() is wrapped by Runnable waiting to be executed by EDT ? – MinhHoang Aug 22 '12 at 19:38
  • No, the `Runnable` invokes the listener's `actionPerformed()` method. Break anywhere in the `actionPerformed()` to see the stack trace. I've added an example above. – trashgod Aug 22 '12 at 19:42
4

By default, if the exception is not caught, the stack trace is written to the console output. The GUI as a whole does not become unresponsive (but, as trashgod commented, the particular component can remain in an unnatural-looking state), the EDT continues to work: Does the EDT restart or not when an exception is thrown?

The good practice is to set up an uncaught exception handler, because you want to know if something went wrong. Note that (depending on the Java version) this might function differently for the EDT than for other threads:

How can I detect when an Exception's been thrown globally in Java?

Note that the "sun.awt.exception.handler" trick, mentioned in many SO posts, is not necessary and does not work in Java 7. For Java 7 just use the standard Thread.setDefaultUncaughtExceptionHandler. Of course, if you use both mechanisms to register the exception handler, the code will work in all versions.

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • I still have one doubt, if unchecked exception is thrown and EDT continues to work so what happen with the rest of codes that rely on returned value from statement that threw unchecked exception? Is program demolished ? – MinhHoang Aug 22 '12 at 17:57
  • 1
    @MinhHoang: The EDT is restarted, but a particular component may appear unresponsive, as suggested in this [example](http://stackoverflow.com/q/3020757/230513) cited above. – trashgod Aug 22 '12 at 18:41
  • @trashgod so before restarting does EDT thread shutdown all components that were previously displayed or those components become unresponsive components ? – MinhHoang Aug 22 '12 at 19:07