Java catch multiple exceptions

If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch block.

Multiple Catch Blocks : Catching Multiple Exceptions

 
  public class ExceptionExample {
	public static void main(String argv[]) {
		int num1 = 10;
		int num2 = 0;
		int result = 0;
		int arr[] = new int[5];
		try {
			arr[0] = 0;
			arr[1] = 1;
			arr[2] = 2;
			arr[3] = 3;
			arr[4] = 4;
			arr[5] = 5;
			result = num1 / num2;
			System.out.println("Result of Division : " + result);
		} catch (ArithmeticException e) {
			System.out.println("Err: Divided by Zero");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Err: Array Out of Bound");
		}
	}

}

Output :

 
  Err: Array Out of Bound 
  

In the above example we have two lines that might throw an exception i.e

arr[5] = 5;

above statement can cause array index out of bound exception and

result = num1 / num2;

this can cause arithmetic exception. To Handle these two different types of exception we have included two catch blocks for single try block.

 
}catch (ArithmeticException e) {
  System.out.println("Err: Divided by Zero");
 }catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("Err: Array Out of Bound");

}

Now Inside the try block when exception is thrown then type of the exception thrown is compared with the type of exception of each catch block.

If type of exception thrown is matched with the type of exception from catch then it will execute corresponding catch block.

Notes :

  1. At a time only single catch block can be executed. After the execution of catch block control goes to the statement next to the try block.
  2. At a time Only single exception can be handled.
  3. All the exceptions or catch blocks must be arranged in order.
  4. Catch Block must be arranged from Most Specific to Most General :

If we wrote catch block like this

}catch (Exception e) {
     System.out.println("Err: Exception Occurred");

}catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Err: Array Out of Bound");
}

  1. All the exceptions will be catched in the First Catch block because Exception is superclass of all the exceptions.
  2. In the above program ArrayIndexOutOfBoundsException is an subclass of Exception Class.

Writing above code will throw following error message after compiling it -

Unreachable catch block for ArrayIndexOutOfBoundsException.It is already handled by the catch block for Exception

Rule: At a time only one Exception is occured and at a time only one catch block is executed.

Rule: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception .

nested-try-block

The try block within a try block is known as nested try block in java.

Why use nested try block

Sometimes situation occurred, where we need to catch multiple conditions.

this chapter we will be learning nested try block. Consider the following snippet a

  Syntax:

  1. ....
  2. try
  3. {
  4. statement 1;
  5. statement 2;
  6. try
  7. {
  8. statement 1;
  9. statement 2;
  10. }
  11. catch(Exception e)
  12. {
  13. }
  14. }
  15. catch(Exception e)
  16. {
  17. }
  18. ....
  public class ExceptionExample {

	public static void main(String argv[]) {
		int result = 0;
		int arr[] = new int[5];
		arr[5] = 5;
		result = 100 / 0;
		System.out.println("Result of Division : " + result);

	}

}
				

In the above example of nested try block we can see that

 arr[5] = 5;

result = 100 / 0; 

Java nested try example

 public class ExceptionExample {

	public static void main(String argv[]) {
		int num1 = 10;
		int num2 = 0;
		int result = 0;
		int arr[] = new int[5];
		try {
			try {
				arr[5] = 5;
			} catch (ArrayIndexOutOfBoundsException e) {
				System.out.println("Err: Array Out of Bound");
			}
			try {
				result = num1 / num2;
			} catch (ArithmeticException e) {
				System.out.println("Err: Divided by Zero");
			}
			System.out.println("Result of Division : " + result);
		} catch (Exception e) {
			System.out.println("Exception Occured !");
		}
	}

}