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

Clarify IAE thrown by Container.add(Component, int)

XMLWordPrintable

    • b95
    • generic, sparc
    • generic, solaris_7

      If components are added to Container using Container.add(Component comp, int
      index), and index is not consecutive, IllegalArgumentException will be thrown.

      Here is an example:

      import java.awt.*;

      public class ContainerAdd {
          public static void main(String[] args) throws Exception {
              Container c = new Container();
              int[] values = {0, 1, 2, 3, 4, 8};
              LWComponent comp = new LWComponent();
              for(int i=0;i<values.length;i++) {
                  try {
                      c.add(new LWComponent(), values[i]);
                  } catch (ArrayIndexOutOfBoundsException e) {
                      System.out.println("Unexpected ArrayIndexOutOfBoundsException, index=" + values[i]);
                  } catch (IllegalArgumentException e) {
                      System.out.println("IllegalArgumentExeption is thrown for index " + values[i]);
                      e.printStackTrace();
                  }
              }
          }
      }

      class LWComponent extends Component {
          public LWComponent() {
          }
      }

      Output in build 1.4.1-beta-b11 is:

      IllegalArgumentExeption is thrown for index 8
      java.lang.IllegalArgumentException: illegal component position
              at java.awt.Container.addImpl(Container.java:605)
              at java.awt.Container.add(Container.java:346)
              at ContainerAdd.main(ContainerAdd.java:10)

      "IllegalArgumentException - if index is invalid" this is unclear. User needs to know what indices are invalid.
      This behavior needs to be documented.
      .

            son Oleg Sukhodolsky (Inactive)
            dav Andrei Dmitriev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: