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

javac reports error when infered type is used as return type

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 8-pool
    • tools
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      $ sw_vers
      ProductName: macOS
      ProductVersion: 13.0
      BuildVersion: 22A380
      $ uname -a
      Darwin SID1B560988 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64 x86_64
      $ java -version
      java version "1.8.0_351"
      Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
      Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      When compiling the SneakyThrow class, javac reports an error for the method using the infered type as return type. Compilation with a Java 11 javac works just fine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      $ /Library/Java/JavaVirtualMachines/jdk1.8.0_351.jdk/Contents/Home/bin/javac SneakyThrow.java


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should compile without errors.
      ACTUAL -
      SneakyThrow.java:4: error: unreported exception Throwable; must be caught or declared to be thrown
              sneakyThrow_error_with_javac_8(new Exception());
                                            ^
      SneakyThrow.java:8: error: unreported exception Throwable; must be caught or declared to be thrown
              throw sneakyThrow_error_with_javac_8(new Exception());
                                                  ^
      SneakyThrow.java:8: error: unreported exception Throwable; must be caught or declared to be thrown
              throw sneakyThrow_error_with_javac_8(new Exception());
              ^
      3 errors

      ---------- BEGIN SOURCE ----------
      public class SneakyThrow {

          void javac8_error() {
              sneakyThrow_error_with_javac_8(new Exception());
          }

          void javac8_error_with_throws() {
              throw sneakyThrow_error_with_javac_8(new Exception());
          }

          void works() {
              sneakyThrow_works_with_javac_8(new Exception());
          }

          @SuppressWarnings("unchecked")
          <T extends Throwable> T sneakyThrow_error_with_javac_8(Throwable t) throws T {
              throw (T) t;
          }

          @SuppressWarnings("unchecked")
          <T extends Throwable> void sneakyThrow_works_with_javac_8(Throwable t) throws T {
              throw (T) t;
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Omit return type of sneakyThrow method but this excludes uses cases such as:
          int fooBar() {
             try {
                 return checkedExceptionThrowingMethod();
             } catch(CheckedException e) {
                 throw sneakyThrow(e);
             }
          }
      When using sneakyThrow for testing purposes it would require more boiler plate code for more complex scenarios when having to omit the return type.
      (Switching to a more recent JDK version is not a valid workaround due to other restrictions.)

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: