Posted by: lrrp | February 17, 2015

How to use Java 1. 7 Multiple Catch Block

Lets  learn how multi catch block of JDK 1.7 makes Exception handling code simpler and elegant. Multiple catch block will allow you to catch multiple exceptions in one block but it’s only available in JDK7 and you need to compiler your code with source 1.7. I also recommend the book Java 7 Recipes: A Problem-Solution Approach to learn more about all the changes made in JDK 1.7 and how to make effective use of them.

JDK 1. 7 feature: Improved exception handling using multi-catch block

Java has always been criticized for having checked exceptions and polluting code with cluttered exception handling code, multi-catch block in Java 1.7  certainly assuage those wounds. With multi catch block,  you can catch multiple exceptions in one catch block, which will eventually result in more readable code.

Prior to JDK 7 if you want to catch two exceptions, you need to provide two catch blocks and if you have same code to run on these two blocks, then either you need to use finally block or just duplicate the code on two catch blocks. finally block is  not an ideal solution, because it will execute even if Exception is not thrown so ultimately lot of duplicate code which sometime makes code unreadable and clumsy.

With JDK7 multi catch block we can catch multiple exception in one catch block separated by pipe (|) and reduce the code duplication.

Let’s see an example of multiple exceptions catching in Java 7.

public static void main(String args[]){

Scanner scnr = new Scanner(System.in);

String number = scnr.next();

try {

if (number.length() > 5) {

throw new IllegalArgumentException();

}

Integer.parseInt(number);

} catch (NumberFormatException | IllegalArgumentException e) {

e.printStackTrace();

}

}

In above code example or JDK7 multi-catch block we have used multiple catch block of JDK 1.7 and control will come on this block whenever code throws either NumberFormatException or IllegalArgumentException.

Java 7  multiple catch block example tutorial

We have seen code making use of this new Java 7 feature of catching more than one Exception in one catch block. In our example, we are catching NumberFormatException and IllegalArgumentException together and her we will verify that by entering input which will result in both type of Exception one by one. If we are able to catch both Exception than it’s proven.

Testing of JDK 1.7 multi-cache block  

If we will enter any number with alphabets, than it will throw NumberFormatException as shown below :

Input: 23ff

java.lang.NumberFormatException: For input string: “23ff”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at jdk7demo.JDK7Demo.main(JDK7Demo.java:25)

Now let’s enter number with more than 5 digit this will result in IllegalArgumentException as per our code.

Input :123333
java.lang.IllegalArgumentException
at jdk7demo.JDK7Demo.main(JDK7Demo.java:23)

Recommended Book for further Reading
Java 7 Recipes: A Problem-Solution Approach By Josh Juneau, Carl Dea, Freddy Guime, John O’Conner


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: