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

JShell : Stack trace of exception has wrong line number

XMLWordPrintable

    • b02
    • x86_64
    • generic

        A DESCRIPTION OF THE PROBLEM :
        For some multiline snippets the JShell produces the wrong line number in the stack trace. This can either be observed directly in the JShell or in EvalExceptions obtained by an instance of the class jdk.jshell.JShell.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Open a jshell console and type the following code

        jshell> if (true) {
           ...> int x = 10;
           ...> int y = 10 / 0;}
        | java.lang.ArithmeticException thrown: / by zero
        | at (#1:1)

        My original example was a snippet from a larger file which had leading newlines (see test case below): "\n\nint y = 10 / 0;"

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The exception should be reported on line 3 of this snippet.

        For example, the following code works as expected:

        jshell> if (true) {
           ...> int x = 10;
           ...> int y = 10 / 0;
           ...> }
        | java.lang.ArithmeticException thrown: / by zero
        | at (#2:3)

        The only difference in this example is the additional line break before the closing brace. However, beyond this example I could not identify any pattern which code leads to errors and which does not.
        ACTUAL -
        The exception is reported on line 1 instead of line 3.

        ---------- BEGIN SOURCE ----------
        import jdk.jshell.JShell;
        public class EvalLines {
            public static void main(String[] args) {
                JShell shell = JShell.create();
                shell.eval("\n\nint y = 10/0;").get(0).exception().printStackTrace();
                System.out.println("-----");
                System.out.println(shell.snippets().findFirst().get().source());
                System.out.println("-----");
            }
        }
        ---------- END SOURCE ----------

        FREQUENCY : always


              rfield Robert Field (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: