How to use jdk7 multi-cache block with example

Tutorials Shared by the Internet Community

Advertisements


Synopsis

project coin also called as Small language enhancements or JSR 334. Though there were not major changes but they are very useful in terms of simplifyi

Statistics

  • Total Hits - 26947

  • Total Votes - 96 votes

  • Vote Up - 41 votes

  • Vote Down - 55 votes

  • Domain - javarevisited.blogspot.com

  • Category - JAVA/Core JAVA

  • Submitted By - Javin Paul

  • Submitted on - 2011-08-10 14:49:45

Description

Java has always been criticized for having checked exception and polluting code with cluttered exception handling code, multi-cache block in JDK certainly assuage those wounds. With multi-cache block you can catch multiple exceptions in one block which will eventually result in less code and more readable code. Prior to JDK7 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 or just duplicate the code on two catch blocks. finally 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. Now 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 JDK7 ...


Site  |   Broken  |   Tweet  |   Facebook  |     |   Save  |   Liked  |   Down

Advertisements