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

Incorrect line number reported in exception stack trace thrown from a lambda expression

XMLWordPrintable

    • b27

        Consider the following class:
        ================================================
        import java.util.stream.LongStream;

        public class TestLambdas {
            public static void main(String[] args) {
                new TestLambdas().execute(0, 5);
            }

            public boolean assertLong(long l, long forbidden) {
                if (l == forbidden) {
                    throw new AssertionError(forbidden);
                }
                return true;
            }

            public boolean assertLong2(long l, long forbidden) {
                if (l == forbidden) {
                    throw new AssertionError(forbidden);
                }
                return true;
            }

            public void execute(long first, long last) {
                for (int i = 0; i < 1000; i++) {
                    long forbidden = 4 + i;
                    System.out.println("first");
                    LongStream.range(first, 4).forEachOrdered((l) ->
                            assertLong(l, forbidden)
                    );
                    System.out.println("Second");
                    LongStream.range(first, last).forEachOrdered((l) ->
                            assertLong(l, forbidden)
                    );
                    System.out.println("Done");
                }
            }
        }
        ================================================
        An exception should be raised by the second lambda invocation, at line 31.

        But the exception that gets printed says that it happened at line 27, which is not possible:

        Exception in thread "main" java.lang.AssertionError: 4
        at TestLambdas.assertLong(TestLambdas.java:10)
        at TestLambdas.lambda$execute$0(TestLambdas.java:27)
        at java.base/java.util.stream.Streams$RangeLongSpliterator.forEachRemaining(Streams.java:228)
        at java.base/java.util.stream.LongPipeline$Head.forEachOrdered(LongPipeline.java:610)
        at TestLambdas.execute(TestLambdas.java:30)
        at TestLambdas.main(TestLambdas.java:5)

        If run in the IDE instead the same code reports the correct location:

        Exception in thread "main" java.lang.AssertionError: 4
        at TestLambdas.assertLong(TestLambdas.java:10)
        at TestLambdas.lambda$execute$1(TestLambdas.java:31)
        at java.base/java.util.stream.Streams$RangeLongSpliterator.forEachRemaining(Streams.java:228)
        at java.base/java.util.stream.LongPipeline$Head.forEachOrdered(LongPipeline.java:610)
        at TestLambdas.execute(TestLambdas.java:30)
        at TestLambdas.main(TestLambdas.java:5)

              cushon Liam Miller-Cushon
              dfuchs Daniel Fuchs
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated:
                Resolved: