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

More user friendly compile-time errors for uncaught exceptions in lambda expression

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8
    • 8
    • tools
    • $ javac -fullversion
      javac full version "1.8.0-ea-b91"

    • b105
    • Verified

      Consider this program:
      -----
      package test;

      import java.io.FileInputStream;
      import java.io.InputStream;
      import javax.swing.SwingUtilities;

      public class Test {

          public static void main(String[] args) {
              SwingUtilities.invokeLater(() -> {
                  InputStream in = new FileInputStream("");
                  
                  in.read();
              });
          }
      }
      -----

      Compiling this class yields this compile-time error:
      Test.java:10: error: incompatible thrown types IOException in lambda expression
              SwingUtilities.invokeLater(() -> {
                                         ^
      1 error

      Which, while correct, does not point the user to the place where the problem(s) appear, and is inconsistent with compile-time errors reported for similar problems outside lambda expressions.

      As an example, consider this program:
      -----
      package test;

      import java.io.FileInputStream;
      import java.io.InputStream;

      public class Test {

          public static void main(String[] args) {
              InputStream in = new FileInputStream("");

              in.read();
          }
      }
      ----

      The compile-time errors produced for this class are:
      Test.java:9: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
              InputStream in = new FileInputStream("");
                               ^
      Test.java:11: error: unreported exception IOException; must be caught or declared to be thrown
              in.read();
                     ^
      2 errors

      These errors point out the problematic places. It would be nice if similar errors could be produced when the uncaught exceptions are inside the lambda expression.

      Tested on:
      $ javac -fullversion
      javac full version "1.8.0-ea-b91"

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: