Home  >  Article  >  Java  >  How to handle exceptions in JShell in Java 9?

How to handle exceptions in JShell in Java 9?

王林
王林forward
2023-09-22 19:21:041282browse

在Java 9中如何处理JShell中的异常?

In Java 9, JShell provides a fast and friendly environment that allows us to quickly explore, discover and experiment with Java language features and richness library.

In JShell, there is no need to manually catch exceptions. JShell Automatically catches each exception and displays the relevant information, then displays the next JShell prompt so we can continue the session. It also works with uncheckedExceptions. JShell makes it easier to experiment with methods that throw checked exceptions by automatically catching checked and unchecked exceptions.

In the example below, an ArrayIndexOutOfBoundsException occurs because the value for "values[4]" is not found.

Example-1

<strong>jshell> int[] values = {10, 20, 30}
values ==> int[3] { 10, 20, 30 }

jshell> values[4]
|   java.lang.ArrayIndexOutOfBoundsException thrown: 4
|        at (#7:1)</strong>

In the example below, a FileNotFoundException## occurs due to a file not being found in the directory. #. Example 2

<strong>jshell> FileInputStream fis = new FileInputStream("data.txt")
| java.io.FileNotFoundException thrown: data.txt (The system cannot find the file specified)
|       at FileInputStream.open0 (Native Method)
|       at FileInputStream.open (FileInputStream.java:196)
|       at FileInputStream. (FileInputStream.java:139)
|       at FileInputStream. (FileInputStream.java:94)
|       at (#5:1)</strong>

In the following example, since "

1/0" is not definition.

Example 3

<strong>jshell> 1/0
|  java.lang.ArithmeticException thrown: / by zero
|        at (#4:1)</strong>

The above is the detailed content of How to handle exceptions in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete