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

How to handle exceptions in JShell in Java 9?

王林
Release: 2023-09-22 19:21:04
forward
1393 people have browsed it

在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>
Copy after login

In the example below, a FileNotFoundException## occurs due to a file not being found in the directory. #. < /p>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>
Copy after login

In the following example, since "

1/0< strong>" is not definition.

Example 3

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template