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

Javac fails to compile code with multiple generic exceptions

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 11.0.3
    • Fix Version/s: None
    • Component/s: tools
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      ADDITIONAL SYSTEM INFORMATION :
      uname -a
      Linux abramczykg-ThinkPad-P51 4.15.0-51-generic #55-Ubuntu SMP Wed May 15 14:27:21 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
      java --version
      openjdk 11.0.2 2019-01-15
      OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
      OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      I encountered construction that causes javac to fail with error 'error: unreported exception Exception; must be caught or declared to be thrown'.
      Extracting lambda to variable fixes the error so I believe this is issue with the compiler.

      The example attached is standalone equivalent of real code where the issue was first encountered.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run javac on the following source

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code compiles.
      ACTUAL -
      src/java_error/Example.java:36: error: unreported exception Exception; must be caught or declared to be thrown
      Optional.of("a").map(a -> execute(a, operation))
      ^
      1 error


      ---------- BEGIN SOURCE ----------

      package java_error;

      import java.util.Optional;
      import java.util.function.Function;

      public class Example {

      interface Operation<I, O, E extends Exception> {
      O execute(I input) throws E;
      }

      interface ErrorHandler<I, O, E extends Exception, E2 extends Exception> {
      O handle(I input, E error) throws E2;
      }

      public <I, O, E extends Exception, E2 extends Exception> O execute(I input,
      Operation<I, O, E> function) throws E, E2 {
      return execute(input, function, (i, e) -> {
      throw e;
      });
      }

      public <I, O, E extends Exception, E2 extends Exception> O execute(I input,
      Operation<I, O, E> function, ErrorHandler<I, O, E, E2> handler) throws E, E2 {
      return function.execute(input);
      }

      Operation<String, String, RuntimeException> operation = a -> a;

      public void ok() {
      Function<? super String, ? extends String> mapper = a -> execute(a, operation);
      Optional.of("a").map(mapper).orElseThrow(() -> new IllegalArgumentException("Missing optional"));
      }

      public void notOk() {
      Optional.of("a").map(a -> execute(a, operation))
      .orElseThrow(() -> new IllegalArgumentException("Missing optional"));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Extracting the mapping function to separate variable fixes the error.

      FREQUENCY : always

        Attachments

          Activity

            People

            • Assignee:
              adev ANUPAM DEV
              Reporter:
              tongwan Andrew Wang
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: