

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to handle an exception in JShell in Java 9?
In Java 9, JShell provides a fast and friendly environment that enables us to quickly explore, discover, and experiment with Java language features and extensive libraries.
In JShell, manually catching the exceptions is not required. JShell automatically catches each exception and displays information about it, then displays the next JShell prompt so we can continue our session. It works for unchecked exceptions also. By automatically catching both checked and unchecked exceptions, JShell makes it easier for us to experiment with methods that throw checked exceptions.
In the below example, ArrayIndexOutOfBoundsException has occurred because the value of "values[4]" not found.
Example-1
jshell> int[] values = {10, 20, 30} values ==> int[3] { 10, 20, 30 } jshell> values[4] | java.lang.ArrayIndexOutOfBoundsException thrown: 4 | at (#7:1)
In the below example, FileNotFoundException has occurred because the file not found in the directory.
Example-2
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)
In the below example, ArithmeticException (unchecked exception) has occurred because the value of "1/0" is undefined.
Example-3
jshell> 1/0 | java.lang.ArithmeticException thrown: / by zero | at (#4:1)
- Related Questions & Answers
- JShell in Java 9?
- How to initialize an array in JShell in Java 9?
- How to debug JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to handle an exception in Python?
- How to handle an exception in JSP?
- How to handle an exception using lambda expression in Java?
- How to get JShell documentation in Java 9?
- How to handle the Runtime Exception in Java?
- How to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?
- How to declare a class and an interface in JShell in Java 9?
- How JShell tool works internally in Java 9?