JDK-8212167 : JShell : Stack trace of exception has wrong line number
  • Type: Bug
  • Component: tools
  • Sub-Component: jshell
  • Affected Version: 9,10,11,12
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86_64
  • Submitted: 2018-10-12
  • Updated: 2018-10-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
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



Comments
Issue is reproducible in JDK 9, 11 and 12-ea+15. JShell produces the wrong line number in the stack trace for the ArithmeticException when snippet is created directly or using eval(String) method in JShell. But putting last closing brace in newline gives proper line number in stacktrace. jshell> if (true) { ...> int x = 10; ...> int y = 10 / 0; ...> } | Exception java.lang.ArithmeticException: / by zero | at (#1:3)
2018-10-15