Thursday, January 17, 2013

Java 7 Improved Exception Handling - Multiple Catch block

Java 7 in many ways improved exception handling. Two of the feature of Java 7 which improves exception handling are ability to catch multiple exception 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 Exception in Java. Programmers always complained that it clutter code and reduced readability. Java 7 some how reduces this pain by improve Exception handling feature e.g. multiple catch 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 Exception 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 handle in one catch block prior to Java 7. Though there is an alternative to catch java.lang.Exception instead of java.io.IOException and subsequent 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 exception, its not considered as good practice. With multiple exception catch block in Java 7, you can combine handling of these two exception at one place in 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.
}

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

Other Java 7 tutorials for Java programmer

No comments:

Post a Comment

Java67 Headline Animator