How to use Java 1. 7 Multiple Catch Block with example - Tutorial (original) (raw)

As the release of JDK 7 approaching General Availability (GA) on 2011/07/28, I thought to have a gorftlook on language enhancement as part of project coin, also called as Small language enhancements or JSR 334. Though there are not any major changes like Enum or Generics of Java 1.5, but they are still very useful, in terms of simplifying your day to day programming task. Some of the interesting changes are allowing String in Switch cases, inclusion of fork-join framework in JDK itself , type inference using a diamond operator, automatic resource management using try with resource feature, and ability to catch multiple Exception in the single catch block .

In this Java 7 tutorial, we will 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 compile your code with source 1.7.

This article also shows you how to use JDK 7 multiple catch block with an example. I also recommend book Java 7 Recipes: A Problem-Solution Approach to learning more about all the changes made in JDK 1.7 and how to make effective use of them.g

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

jdk7 multi-cache block example tutorialJava has always been criticized for having checked exception 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 some code to run on these two blocks, then either you need to use finally block or just duplicate the code on two catch blocks.

The finally block is not an ideal solution because it will execute even if an Exception is not thrown so ultimately a lot of duplicate code which sometimes makes code unreadable and clumsy. Now with JDK7 multi catch block, we can catch multiple exceptions in one catch block separated by a 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 the above code example or JDK7 multi-catch block, we have used multiple catch blocks of JDK 1.7 and control will come on this block whenever code throws either NumberFormatException or IllegalArgumentException.

Java multiple catches 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 we will verify that by entering an input which will result in both types of Exceptions one by one. If we are able to catch both Exceptions then it's proven.

Testing of JDK 1.7 multi-cache block

If we will enter any number with alphabets, then 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 a number with more than 5 digits this will result in IllegalArgumentException as per our code.

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

I used Netbeans 7 to compile and run this project. Setting up JDK 7 in Netbeans is very easy just download JDK7 and then click on Tool-->Java Platform and then click "Add Platforms" it will open a file browser just point out the JDK7 installation directory and it will import JDK 1.7 binaries, source, and docs and set it up for your use.

One more thing you need to remember is that setting the source as 1.7 because this new language feature is only available in JDK7. In the next series of this JDK7 feature article, we will see how to use String in the Switch statement.