
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 this article, we will learn to handle an exception in JShell in Java 9. We will learn about the JShell, exceptions in Java, JShell exceptions, their handling, and Types of exceptions in JShell with examples.
What is JShell?
JShell is a new command-line interactive REPL (Read-Evaluate-Print-Loop) tool introduced in Java 9 to evaluate declarations, statements, and expressions written in Java. This tool also allows us to execute Java code snippets and get immediate results.
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell>
JShell provides a fast and friendly environment that enables us to quickly explore, discover, and experiment with Java language features and extensive libraries.
What is an Exception?
An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runtime). When an Exception occurs the normal flow of the program is broken and the program terminates, which is not recommended, therefore, these exceptions are to be handled.
What is JShell Exception?
JShellException is a specific type of exception that occurs when an error is encountered while executing code snippets within the JShell environment.
Exception Handling in JShell
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.
Types of JShellExceptions
Below are the common types of JShell Exceptions in Java 9:
-
ControlStatementException: Errors related to control statements like if, while, or for come under the ControlStatementException error.
- ParsingException: Usually occurs due to syntax errors, when the code snippet cannot be parsed successfully.
- ValueException: This error arises when trying to assign invalid values to variables.
Example 1
In the below example, an ArrayIndexOutOfBoundsException has occurred because the value of "values[4]" was not found.
jshell> int[] values = {10, 20, 30} values ==> int[3] { 10, 20, 30 } jshell> values[4] | java.lang.ArrayIndexOutOfBoundsException thrown: 4 | at (#7:1)
Example 2
In the below example, a FileNotFoundException has occurred because the file was not found in the directory.
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)
Example 3
In the below example, ArithmeticException (an unchecked exception) has occurred because the value of "1/0" is undefined.
jshell> 1/0 | java.lang.ArithmeticException thrown: / by zero | at (#4:1)