
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- How to initialize an array in 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?\n
- How to implement an ArrayList using JShell in Java 9?
- How to handle the Runtime Exception in Java?
- How to debug JShell in Java 9?
- JShell in Java 9?
- How to handle the exception using UncaughtExceptionHandler in Java?
- How to get JShell documentation in Java 9?
- How to declare a class and an interface in JShell in Java 9?
- 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 import external libraries in JShell in Java 9?
