Jshell continues to execute my script after throwing an exception. How to make him stop?

I tried to write some jshell scripts. When an exception is thrown, jshell continues to execute the next line.

How can I make my scripts behave like regular Java programs?

Edit : I just run it as jshell SCR.jsh.

Using throw new Exception()or 1/0does not prevent the execution of the next line.

The script includes the following sentence:

System.out.println(1/0)
System.out.println("foo")
/exit

I thought the second line was unattainable. This is what I expected. But after printing the exception is printed foo.

+4
source share
2 answers

, , jshell script , , script Snippet.

. , , :

1
a
x -> x+1
(String s) -> s.length()

,

System.out.println("Hello world");
new BufferedReader(new InputStreamReader(System.in))

, , Read-Eval-Print Loop (REPL). , Snippet, java.lang.ArithmeticException .

.

(ClassDeclaration, InterfaceDeclaration, MethodDeclaration, FieldDeclaration) - , , .

+6

, , :

{
    System.out.println(1/0);
    System.out.println("foo");
}
/exit

java-.

, , .

+2

Source: https://habr.com/ru/post/1686176/


All Articles