Catch multiple exceptions before & after java 7 (examples)

What is exception (background)?

  • Exception occurs in an application mostly due to programming mistakes.
  • e.g. NullPointerException occurs, when we try to access uninitialized objects
  • e.g. ArrayIndexOutOfBoundsException occurs, when we try to access the array index beyond its bounds.
  • We catch the exceptions using catch blocks and recover from them.
    • e.g. If ArrayIndexOutOfBoundsException occurs, in catch block we can introspect the error condition, correct the exception condition and resume the normal flow of application.
  • If we are expecting multiple exceptions, we can catch exceptions using catch blocks.
  • We will catch multiple exceptions with & without using java 7 feature.
    • Java 7 has introduced the multi catch block feature.
  • Catch multiple exceptions without using Java 7 feature (prior to java 7)

Catch multiple exceptions in java (prior to java7)

try {
    /* Code can throw NullPointerException or IOException exception,
           we are catching individual exceptions. */
} catch (FileNotFoundException e) {
    System.out.println("1.1. Exception FileNotFoundException, message: " + e.getMessage());
} catch (IOException e) {
    System.out.println("1.2. Exception IOException, message: " + e.getMessage());
}

Catch multiple exceptions in single catch block using Java 7

  • We will catch multiple exceptions using single catch block.
  • Each exceptions should be separated by pipe or vertical bar (|).
  • If catch block handling more than one exception then exception parameter implicitly becomes final parameter.
  • The exception types in multiple catch block must be disjoint, one exception cannot be a subclass of another exception.
try {
    /* Code can throw NullPointerException or IOException exception,
           catching exceptions in single block.*/
}
/* Catching multiple exceptions in same block. */
catch (NullPointerException | IOException e) {
    System.out.println("3.3. Exception NullPointerException, message: " + e.getMessage());
}

Program – catch multiple exceptions before & after java 7

package org.learn.client;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
public class CatchMultiExceptionsDemo {
 
    public static void main(String[] args) {
        System.out.println("1. Catching multiple exception prior to java 7:");
        catchMultipleExceptionsPriorJava7();
        System.out.println("\n2. Catching multiple exception after java 7:");
        catchMultipleExceptionsAfterJava7();
    }
 
    private static void catchMultipleExceptionsPriorJava7() {
 
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader("NonExistedFilePath.txt"));
            in.read();
        } catch (FileNotFoundException e) {
            System.out.println("1.1. FileNotFoundException,message: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("1.2. IOException, message: " + e.getMessage());
        } finally {
            try {
                in.close();
            } catch (NullPointerException e) {
                System.out.println("1.3 NullPointerException,message: " + e.getMessage());
            } catch (IOException e) {
                System.out.println("1.4. IOException,message: " + e.getMessage());
            }
        }
    }
 
    private static void catchMultipleExceptionsAfterJava7() {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader("NonExistedFilePath.txt"));
            in.read();
        }
        /*
         * Exceptions should be disjoint, FileNotFoundException is sub class of
         * IOException so, we can not catch FileNotFountException & IOException
         * in same catch block.
         */ catch (FileNotFoundException e) {
            System.out.println("2.1 FileNotFoundException,message: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("2.2 IOException,message: " + e.getMessage());
        } finally {
            try {
                in.close();
            }
            /* Catching multiple exceptions in same block. */
            catch (NullPointerException | IOException e) {
                System.out.println("2.3 Exception occurred,message: " + e.getMessage());
            }
        }
    }
}

Output – catch multiple exceptions before & after java 7

1. Catching multiple exception prior to java 7:
1.1. FileNotFoundException,message: NonExistedFilePath.txt (The system cannot find the file specified)
1.3 NullPointerException,message: null
 
2. Catching multiple exception after java 7:
2.1 FileNotFoundException,message: NonExistedFilePath.txt (The system cannot find the file specified)
2.3 Exception occurred,message: null