JDK 7 multiple catch blocks

In JDK 7, we have feature to integrate multiple catch blocks into one for more code readability and simplicity.


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();
}
}

Advertisements

One Comment Add yours

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