Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5076724

GUI apps can't handle exceptions - ThreadGroup.uncaughtException never called

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • 6
    • 6
    • deploy
    • x86
    • windows_xp

      Name: gm110360 Date: 07/21/2004


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      IA32, Windows XP, and almost certainly all other platforms

      A DESCRIPTION OF THE PROBLEM :
      Swing applications tend to do most of their work in the event thread. Since it is ridiculous to wrap every method that may be run this way in a try/catch, the common solution is to create a ThreadGroup overriding uncaughtException to gracefully handle exceptions, and start your GUI from within it.

      In some cases, this is a critically important safety feature. Applications may need to shut themselves down in the event of unexpected inconsistencies in order to prevent serious problems from occurring.

      This pattern is widely documented in the trade press and tutorial sites, and is in common usage.

      It does not work in Java Web Start. This is cruelly ironic, since Java Web Start seems like it should be the ideal way to deploy swing applciations in Java.

      If the Swing application performs any important task where handling unexpected problems is necessary for safety, Java Web Start should not be used until this bug is fixed.

      Additional description of the problem (and frightening workaround):

      http://lhankins.blogspot.com/#390328764

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a Swing hello world application. In main(), start it from within a ThreadGroup defining uncaughtException , i.e.:

      http://www.javaspecialists.co.za/archive/Issue081.html

      Throw an exception in any method run in the event thread. Observe that the uncaughtException code is called.

      Run the same application in Java Web Start.

      Observe that the uncaughtException code is not called.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The ThreadGroup's uncaughtException method should be called from within Java Web Start.

      This enables sane, concise exception handling in a GUI application.
      ACTUAL -
      The ThreadGroup's uncaughtException method is not called from within Java Web Start.

      It is now impossible to reliably handle problems that arise in the event thread. You can try to wrap every method that could be executed from the event thread in a try/catch, but this is hideously ugly and in a "real sized" application you can't be sure you haven't missed something.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      There are no messages indicating that this behavior will occur or is occurring.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.event.MouseListener;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;

      public class SwingExceptionTest
      {
         public static void main(String[] args)
         {
            // We work from a new thread in our custom ThreadGroup in order to trap
            // uncought exceptions. In an application where failures are dangerous,
            // we use this to go into "safety mode" if we observe any exceptions.
            ThreadGroup exceptionhandler = new ExceptionMonitoredGroup();
            new Thread(exceptionhandler, "Initialization Thread") {
               public void run()
               {
                  JFrame window = new JFrame("test");
                  window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  window.getContentPane().setLayout(new BoxLayout(window.getContentPane(),BoxLayout.X_AXIS));
                  window.getContentPane().add(new JLabel("Click Me"));

                  window.addMouseListener(new BadClass());

                  window.setBounds(100,100,200,200);
                  window.show();
               }
            }.start();
         }

         private static class BadClass extends MouseAdapter implements MouseListener
         {
            public void mouseClicked(MouseEvent e)
            {
               // This is run in the AWT event thread.
               throw new RuntimeException("This exception should be handled by the ThreadGroup's overridden uncaughtException, but in Java Web Start it is not.");
            }
         }

         private static class ExceptionMonitoredGroup extends ThreadGroup
         {
            public ExceptionMonitoredGroup()
            {
               super("ExceptionMonitoredGroup");
            }

            /**
             * Here we would ordinarily notify the user with a status message, and
             * then lock the application.
             */
            public void uncaughtException(Thread t, Throwable e)
            {
               System.err.println("uncaughtException successfully called!");
               e.printStackTrace();
            }
         }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      This is a tough one. The ThreadGroup uncaughtException appears to only work if the AWT thread is started from within that group. The AWT thread starts whenever you touch anything Swing/AWT related. This is easy to manage in an ordinary application, but in Java Web Start, we're never the first GUI code to run, so we can't use this technique.

      A blogger seems to have found a workaround using an undocumented AWT feature. I haven't tested it yet. Even if it works, there really should be a better way! This is Java Web Start - running AWT/Swing clients is the whole point!

      http://lhankins.blogspot.com/#390328764
      (Incident Review ID: 240001)
      ======================================================================

            ngthomas Thomas Ng (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: