EDT and runtime exception

What happens if any method called from the EDT thread throws an unchecked exception? Does it keep responsive GUI or what? Thanks you

+2
source share
2 answers

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

It is good practice to create an exception handler because you want to know that something went wrong. Please note that (depending on the Java version) this may work differently for EDT than for other threads:

How to determine when an exception was thrown in Java?

Note that the trick "sun.awt.exception.handler" mentioned in many SO posts is not required 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 an exception handler, the code will work in all versions.

+4
source

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

No, EDT simply resumes execution of Runnable instances, as it was before; broken Runnable keep throwing exceptions. As an exercise, consider the example in the debugger.

Appendix: Here is a typical stack trace from this.

  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.javahaps87)
 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)
+5
source

Source: https://habr.com/ru/post/924965/


All Articles