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 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 Exception in Java. Programmers always complained that it clutter the code and reduced readability. Java 7 somehow reduces this pain by improving 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 handled in one catch block prior to Java 7.

Though there is an alternative to catch java.lang.Exception instead of 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 exception, 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.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:

}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  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 which 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.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

Other Java 7 tutorials for Java programmer

No comments:

Post a Comment

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