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

Spurious error about generic exception not caught

XMLWordPrintable

    • x86_64
    • linux

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      When compiling the following class:

      public class ThrowableParameters {
          public static void main(String[] args) {
              Collections.singletonList(Arrays.asList("a", "b", "c").stream()
                      .findAny()
                      .orElseThrow(() -> new IllegalArgumentException("?")));
          }
      }

      the compiler reports an error like this:

      src/test/bugs/ThrowableParameters.java:10: error: unreported exception X; must be caught or declared to be thrown
                      .orElseThrow(() -> new IllegalArgumentException("?")));
                                  ^
        where X,T are type-variables:
          X extends Throwable declared in method <X>orElseThrow(Supplier<? extends X>)
          T extends Object declared in class Optional
      1 error

      This does not happen if the outer invocation of Collections.singletonList() is removed.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should compile without errors.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package test.bugs;

      import java.util.Arrays;
      import java.util.Collections;

      public class ThrowableParameters {
          public static void main(String[] args) {
              Collections.singletonList(Arrays.asList("a", "b", "c").stream()
                      .findAny()
                      .orElseThrow(() -> new IllegalArgumentException("?")));
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The parameter to Collections.singletonList() can be first assigned to a local variable.

      The orElseThrow() method can have explicit type parameters added, like this:
                       .<IllegalArgumentException> orElseThrow(() -> new IllegalArgumentException("?")));

      In both cases the class compiles without errors.

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: