Catching Multiple Exceptions in Java 7 is a very good change Oracle had made in Exception Handling to avoid duplication of code.

Catching Multiple Exceptions in Java 7

Java 7 has introduced multi-catch functionality in a single catch block to avoid redundancy of catching multiple exceptions that has the same action associated with them. For Java 6 and earlier we used to repeat the action in catch block for every exception though the action is the same for multiple exceptions.

Example:

MultiCatchJava6.java
public class MultiCatchJava6 {
    public static void main(String as[]) {
        try {
            processRequest();
        } catch (FileNotFoundException fnfe) {
            sendMailToFunctionalTeam(fnfe);
        } catch (NullPointerException npe) {
            sendMailToTechnicalTeam(npe);
        } catch (ArrayIndexOutOfBoundsException aioe) {
            sendMailToTechnicalTeam(aioe);
        } catch (NumberFormatException nfe) {
            sendMailToFunctionalTeam(nfe);
        }
    }
}

Explanation:

Here whenever we have FileNotFoundException and NumberFormatException, those are indications of having something wrong with the input and hence we have to send mail to the functional team to inform about it.

In other cases like NullPointerException and ArrayIndexOutofBoundsException, we have to inform the technical team to take action on the issue. Here we are performing some action for two different exceptions, making code redundant.

Catching Multiple Exceptions

From Java 7 we can catch multiple exceptions in the single catch block. Exceptions have to separate by | (pipe) symbol. With Java 7 above code, can be refactored to:

Example:

MultiCatchJava7.java

public class MultiCatchJava7 {
    public static void main(String as[]) {
        try {
            processRequest();
        } catch (FileNotFoundException | NumberFormatException functionalExc) {
            sendMailToFunctionalTeam(functionalExc);
        } catch (NullPointerException | ArrayIndexOutOfBoundsException technicalExc) {
            sendMailToTechnicalTeam(technicalExc);
        }
    }
}

Explanation:

As shown above we are catching technical exceptions NullPointerException and ArrayIndexOutofBoundsException in a single catch block to send mail to the technical team and FileNotFoundException and NumberFormatException in one catch block to send mail to the functional team. This will avoid writing redundant code and maintain clean code.

Points to Remember with regarding Catching Multiple Exceptions in Java 7:

  • The exception object in the catch block having multiple exceptions is always final. You can’t modify values of the variable within a catch block. If you want to do so, you can throw it and later catch, check the type of exception object and can modify it.
  • You cannot catch both parent and child class exceptions in single catch block like FileNotFoundException and IOException because The exception FileNotFoundException is already caught by the alternative IOException.
  • Bytecode generated for Catching Multiple Exceptions in Single catch block will be smaller as it reduces the redundancy of both exception handlers and duplication

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment