As per my understanding, the reason why jshell
executes all the lines in your script even after one throws an Exception is since it treats your script as a list of Snippet
.
All expressions are accepted as snippets. This includes expressions
without side effects, such as constants, variable accesses, and lambda
expressions:
1
a
x -> x+1
(String s) -> s.length()
as well as expressions with side effects, such as assignments and
method invocations
System.out.println("Hello world");
new BufferedReader(new InputStreamReader(System.in))
So even one of the snippet throws an exception, the others must follow the Read-Eval-Print Loop(REPL) pattern. As also answered yourself converting the code as a block of statement marks it as a single Snippet
which when throws the java.lang.ArithmeticException
marks its completion thereby.
Though ideally, such statements should be instead defined as a declaration snippet.
A declaration snippet (ClassDeclaration
, InterfaceDeclaration
,
MethodDeclaration
, or FieldDeclaration
) is a snippet that explicitly
introduces a name that can be referred to by other snippets.