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

ThreadGroup.uncaughtExceptionHandler doesn't work

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b49
    • CPU:
      generic, x86
    • OS:
      generic, windows_2000
    • Verification:
      Verified

      Description

      Run the provided test case. With 5.0 it will throw an exception and show a modal dialog with the message when it catches it in ThreadGroup.uncaughtExceptionHandler. With 6.0 this doesn't happen, exception is shown in the console instead.

      import javax.swing.*;
      import java.awt.*;

      public class ExceptionGroup extends ThreadGroup {
        public ExceptionGroup() {
          super("ExceptionGroup");
        }
        public void uncaughtException(Thread t, Throwable e) {
           
            JOptionPane.showMessageDialog(findActiveFrame(),
                e.toString(), "Exception Occurred", JOptionPane.ERROR_MESSAGE);
        
        }
        /**
         * I hate ownerless dialogs. With this method, we can find the
         * currently visible frame and attach the dialog to that, instead
         * of always attaching it to null.
         */
        private Frame findActiveFrame() {
          Frame[] frames = JFrame.getFrames();
          for (int i = 0; i < frames.length; i++) {
            Frame frame = frames[i];
            if (frame.isVisible()) {
              return frame;
            }
          }
          return null;
        }
      }

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.ActionEvent;

      public class Gui extends JFrame {
        private static final int[] DAYS_PER_MONTH =
            {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        public Gui() {
          super("GUI Example");
          final JTextArea text = new JTextArea(14, 30);
          getContentPane().add(new JScrollPane(text));
          getContentPane().add(new JButton(new AbstractAction("Calculate") {
            public void actionPerformed(ActionEvent e) {
                if (SwingUtilities.isEventDispatchThread()) {
                    System.out.println("AP in EventDispatchThread()");
                    System.out.println("Thread: " + Thread.currentThread());
                    System.out.println("TG: " + Thread.currentThread().getThreadGroup());
                }
              text.setText("");
              for (int i=0; i<=DAYS_PER_MONTH.length; i++) {
                text.append("Month " + (i+1) + ": " +
                    DAYS_PER_MONTH[i] + "\n");
              }
            }
          }), BorderLayout.NORTH);
        }
        public static void main(String[] args) {
        ThreadGroup exceptionThreadGroup = new ExceptionGroup();
          new Thread(exceptionThreadGroup, "Init thread") {
            public void run() {
              Gui gui = new Gui();
              gui.pack();
              gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              gui.show();
            }
          }.start();

        }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              art Artem Ananiev
              Reporter:
              dmikhalksunw Denis Mikhalkin (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: