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

Multiple method inheritance and throws clauses

    XMLWordPrintable

Details

    • mantis
    • generic
    • generic
    • Verified

    Description



      Name: jl125535 Date: 11/16/2001


      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


      JLS 15.12.2.2 states that if there is more than one maximally specific method
      with the same signature, "the most specific method is considered to throw a
      checked exception if and only if that exception is declared in the throws
      clauses of each of the maximally specific methods."

      In this example, the line i3.foo() has two maximally specific methods to choose
      from: I1.foo() throws E1, E2; and I2.foo() throws E3, E4;. Therefore, the
      resulting choice can only be considered to throw E4, and nothing else, since E4
      is the only exception which is declared in both methods (since E4 is a
      subinterface of E1). This example should compile as legal code:

      $ cat Blah.java

      class E1 extends Exception {}
      class E2 extends Exception {}
      class E3 extends Exception {}
      class E4 extends E1 {}
      interface I1 {
        void foo() throws E1, E2;
      }
      interface I2 {
        void foo() throws E3, E4;
      }
      interface I3 extends I1, I2 {
        // void foo() throws E4;
      }
      class Blah {
        public void foo(I3 i3) {
          try {
            i3.foo();
          } catch (E4 e4) {
          }
        }
      }

      $ javac Blah.java
      Blah.java:17: unreported exception E1; must be caught or declared to be thrown
            i3.foo();
              ^
      1 error

      [One possibility is that the compiler sees I1.foo() as the maximally specific
      method, but the rationale for this choice is unclear - JML]
      (Review ID: 135418)
      ======================================================================

      Attachments

        Activity

          People

            gafter Neal Gafter
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: