How to use jdk7 multi-cache block with example
|Top Tutorials||New Tutorials||Submit||Login||Register|
Total Hits: 2073
Total Votes: 24 votes
Category: JAVA/Core JAVA
Submitted on: 2011-08-10 14:49:45
Submitted By: Javin Paul
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 More detail...