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

SwingUtilities.invokeAndWait not throwing InvocationTargetException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6
    • client-libs



      Name: el35337 Date: 06/04/98


      The documentation for SwingUtilities.invokeAndWait claims that it will throw an InvocationTargetException if the Runnable object throws an uncaught exception during its execution. This doesn't appear to be happening.

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      public class InvokeAndWaitTest
      {
          public static void main( String[] args )
          {
              JFrame frame = new JFrame( "Invoke And Wait Test" );
              JButton run = new JButton( "Run" );
              run.addActionListener( new ButtonHandler() );
              frame.getContentPane().add( run, BorderLayout.SOUTH );
              frame.setSize( 300, 200 );
              frame.setVisible( true );
          }
      }

      // This class will always throw an exception when run.
      class ExceptionRunner implements Runnable
      {
          public void run()
          {
              System.out.println( "running..." );
              throw new RuntimeException();
          }
      }

      class ButtonHandler implements ActionListener
      {
          public void actionPerformed( ActionEvent evt )
          {
              // start a new thread, and run the ExceptionRunner on the AWT Thread

              Runnable swingRunner =
                      new Runnable()
                      {
                          public void run()
                          {
                              try
                              {
                                  SwingUtilities.invokeAndWait( new ExceptionRunner() );

                                  // shouldn't make it here
                                  System.out.println( "shouldn't reach this point" );
                              }
                              catch( java.lang.reflect.InvocationTargetException e )
                              {
                                  System.err.println( "caught exception " + e );
                              }
                              catch( InterruptedException e2 )
                              {
                                  System.err.println( "caught exception " + e2 );
                              }
                          }
                      };
                      
              new Thread( swingRunner ).start();
          }
      }
      (Review ID: 32821)
      ======================================================================

            sswingtrsunw Swingtraq Swingtraq (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: