Monday, May 20, 2024
HomeProgrammingHow to handle multiple exceptions in Java

How to handle multiple exceptions in Java

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)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

бнанс Створити акаунт on The Interview Process in Clark, Pampanga
Binance注册奖金 on Venus in Scorpio