Site icon

Catch multiple exceptions before & after java 7 (examples)

What is exception (background)?

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

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
Exit mobile version