How to handle multiple exceptions in Java:
Let us learn what multiple exceptions are and how to handle multiple exceptions in Java through various examples. We use .to handle exceptions try to catch blocks in Java. We can use a single catch block or we can use multiple catch blocks to handle the exceptions. Let’s learn how to do that with examples.
Handling multiple exceptions with different catch blocks:
This example shows how to handle multiple exceptions using different catch blocks.
class Example {
public static void main(String[] args) {
try {
String n = null;
System.out.println(n.charAt(0));
} catch (NullPointerException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
In this example program we try the character index 0 of the string N. There are two catch blocks to handle the following exceptions:
- NullPointerException And
- ArrayIndexOutOfBoundsException
If you run this program it will run the first exception block i.e NullPointerException as the value of the string N is zero.
If it’s a ArrayIndexOutOfBoundsException for a NullPointerExceptionit will perform the second catch block. For example:
class Example {
public static void main(String[] args) {
try {
String arr[] = new String[4];
String n = null;
System.out.println(arr[10]);
System.out.println(n.charAt(0));
} catch (NullPointerException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
This code executes the second catch block.
Handling multiple exceptions with different catch blocks:
We can merge the two exceptions in the example above into one. We must separate the exceptions with pipe symbol or |. Note that this option is available from Java SE7.
class Example {
public static void main(String[] args) {
try {
String arr[] = new String[4];
String n = null;
System.out.println(arr[10]);
System.out.println(n.charAt(0));
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
It will throw ArrayIndexOutOfBoundsException because this exception is stated first.
Handle multiple exceptions with the Exception class:
Since the Exception class is the base class of all other classes, we can use this class in the catch block. We don’t need to add any other classes with the Exception class, since all other classes are subclasses of this class.
class Example {
public static void main(String[] args) {
try {
String arr[] = new String[4];
String n = null;
System.out.println(arr[10]);
System.out.println(n.charAt(0));
} catch (Exception e) {
e.printStackTrace();
}
}
}
The following exception is printed:
java.lang.ArrayIndexOutOfBoundsException: 10
at Example.main(Example.java:9)
C# Tip: DO NOT use nameof to value constants
Programming in C# involves making numerous decisions to ensure code efficiency and maintainability. One common aspect is dealing with constants,…
Can we run a Java program without the main method
Can we run a Java program without the main method? The mainly method is executed when we execute a class in…
Java program to calculate students grades in 2 different ways
Java program to calculate students grades: In this post we will learn different ways to calculate the figure from a student…
C# Tip: LINQ’s Enumerable.Range to generate a range of consecutive numbers
When you need to generate a series of numbers in ascending order, you can simply use a while loop with…
Java String charAt method explanation with examples
Java String charAt method: The signAt method of Java string is used to find a character at a given index. This…
3 Ways to Convert System.nanoTime to Seconds in Java
How to convert System.nanoTime to seconds in Java: In this post we will learn how to convert System.nanoTime Unpleasant seconds in…