Master java skills

Catch Multiple Exceptions

Catching multiple exceptions using the catch statement in Java 7 allows developers to handle multiple exception types in a single catch block, reducing code redundancy and making the code more concise.

How to use:

By using the pipe symbol |, developers can list multiple exception types that should be caught in the same block. This feature helps to improve the readability and maintainability of Java code by reducing the amount of repetitive code needed to handle multiple exceptions.

Traditional Way of Catching exception:

Prior to Java7, we used multiple catch statements to catch multiple exception.

Syntax:

try {
    // some code that may throw exceptions
} catch (IOException e) {
    // handle IOException
} catch (SQLException e) {
    // handle SQLException
} catch (Exception e) {
    // handle all other exceptions
}

Example:

package com.javatrainingschool;
import java.io.IOException;
import java.sql.SQLException;

public class MultipleCatchException{    
    public static void main(String args[]){    
        try{    
            String array[] = new String[10];    
            array[10] = "Manu"; // Assigning value to an invalid index
        }    
        catch(ArrayIndexOutOfBoundsException e){  
            System.out.println("Array index out of bounds: " + e.getMessage());  
        }    
        catch(Exception e){  
            System.out.println("Error occurred: " + e.getMessage());  
        }    
     }    
}

Output:

Exception occurred: Index 10 out of bounds for length 10

Explanation:
In this program, we create an array of strings with a length of 10. Then, we assign a value to the 11th index of the array, which is invalid and will throw an ArrayIndexOutOfBoundsException.

Using Java7 feature:

Using Java7, we can catch multiple exception using a single catch statement.

Syntax:

try {
    // some code that may throw exceptions
} catch (IOException | SocketException e) {
    // handle IOException or SocketException
} catch (Exception e) {
    // handle all other exceptions
}

Example:

package com.javatrainingschool;
import java.io.IOException;
import java.sql.SQLException;

public class MultipleCatchException{    
    public static void main(String args[]){    
        try{    
            String array[] = new String[10];    
            array[10] = "Manu"; // Assigning value to an invalid index
            int result = 30/0; // Dividing by zero
        }    
        catch(ArrayIndexOutOfBoundsException | ArithmeticException e){  
            System.out.println("Exception occurred: " + e.getMessage());  
        }    
        catch(Exception e){  
            System.out.println("Error occurred: " + e.getMessage());  
        }    
     }    
}

Output:

Exception occurred: Index 10 out of bounds for length 10

Explanation:

In the above program, We first create an array of strings with a length of 10. Then, we assign a value to the 11th index of the array, which is invalid and will throw an ArrayIndexOutOfBoundsException. This exception is caught in the first catch block, which prints the message “Exception occurred: Index 10 out of bounds for length 10”.

Next, we try to divide 30 by 0, which will throw an ArithmeticException. This exception is also caught in the first catch block because we are using a single catch block to catch both ArrayIndexOutOfBoundsException and ArithmeticException.

Therefore, when we run the program, it catches the ArrayIndexOutOfBoundsException and prints the message “Exception occurred: Index 10 out of bounds for length 10” to the console.