How to use Multiple Catch block for Exception handling in Java? Example Tutorial

Java 7 in many ways improved exception handling. Two of the feature of Java 7 which improves exception handling are the ability to catch the multiple exceptions in one catch block and closing resources automatically using Automatic resource management block. Java has long been criticized for its verbose exception handling code, mandatory to handle checked Exceptions in Java. Programmers always complained that it clutters the code and reduced readability. Java 7 somehow reduces this pain by improving the Exception handling feature e.g. multiple catches and ARM blocks. In this Java 7 tutorial, we will see how to catch multiple exceptions in one catch block using JDK7.


How to catch multiple Exceptions in one catch block

How to catch multiple exception in one block - Java 7 exampleOne place where I feel catching multiple exceptions in one catch block helps is Java database connectivity, Java IO, and formatting date in Java. Since most of JDBC code throws checked SQLException, IO-related code throws IOException, and date formatting throws ParseException, which can't be handled in one catch block prior to Java 7.

Though there is an alternative to catch java.lang.Exception instead of java.io.IOException and subsequently checked exceptions but that approach is fragile because it will also catch unintended exceptions.



Catching java.lang.Exception means your code is open for all kind of exceptions, it's not considered as good practice. With multiple exception catch block in Java 7, you can combine handling of these two exceptions at one place in the code.



Code before Java 7
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Java program to catch multiple exception before Java 7
 */

public class BeforeJava7{

    public static void main(String args[]) {
        try {
            File file = new File("test.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedReader bReader = new BufferedReader(new InputStreamReader(fis));
            String str = bReader.readLine();
            SimpleDateFormat format = new SimpleDateFormat("DD/MM/YY");
            Date date = format.parse(str);
         
        } catch (ParseException exception) {
            //code to handle ParseException
        } catch (IOException exception) {
            //code to handle IOException
        }
    }
}


After Java 7, both of catch block can be replaced with a simple multiple exception catch block, as shown below:

try{
   //code
}catch(ParseException | IOException ex) {
   // handle ParseException and IOException here.
}


Here are some more important features from Java 7 release, you can read about them here:

improved exception handling of Java 7


The important point to note is that variable ex is the final variable here and can not be reassigned. Another place where you can use this improved Exception handling feature is to combine two catch blocks that are essentially doing the same job or just a copy-paste. By using Java 7 multiple catch block, you can reduce a lot of boilerplate code in Java.


Other Java 7 tutorials for Java programmer

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.