There has been room for improvement in the way exceptions are handled by using try-catch block. Try with resources and multi catch blocks handle the following concerns while writing a Java program.

Try with resources

1) Earlier finally blocks were used to execute cleanup code like closing of database and file connections. The problem using finally block is that one needs to perform exception handling inside the finally block too. The advantage of using try with resources block is that while coding, we need not care about writing the finally block.

try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }

In the above code, we need not worry about handling the close of buffered file reader which is done in the finally block.

Multi Catch Block

2) One more issue that was faced earlier to JDK 7 was the addition of duplicate code in multiple catch handlers for the same try block. With JDK 7 comes multi catch handler blocks which can allow running the same code when any of the exception classes mentioned in the catch block are executed. Though one could write a method and call it from multiple catch blocks but not all developers were following that and the need was to add some enhancement in the JDK to reduce the amount of duplicate exception handling code.

The following code shows how to use multi-catch blocks for executing the same code when two or more than two exceptions are raised by a try block.

try {
    Path fp = path.toRealPath(true);
} catch (NoSuchFileException | IOException x) {
    System.err.format("%s: no such file or directory%n", path);
    ...
}

We can use checked as well un-checked exceptions in the above catch handler.

Related Posts

Exception Handling features in JDK 7 admin Core Java
There has been room for improvement in the way exceptions are handled by using try-catch block. Try with resources and multi catch blocks handle the following concerns while writing a Java program. Try with resources 1) Earlier finally blocks were used to execute cleanup code like closing of database and...
There has been room for improvement in the way exceptions are handled by using try-catch block. Try with resources and multi catch blocks handle the following concerns while writing a Java program. <h2>Try with resources</h2> 1) Earlier <a href="http://www.javaexperience.com/java-finally-executes-finally/" title="finally executes finally">finally blocks</a> were used to <strong>execute cleanup code</strong> like closing of database and file connections. The problem using finally block is that one needs to <strong>perform exception handling</strong> inside the finally block too. The advantage of using try with resources block is that while coding, we need not care about writing the finally block. 1 In the above code, we need not worry about handling the close of buffered file reader which is done in the finally block. <h2>Multi Catch Block</h2> 2) One more issue that was faced earlier to JDK 7 was the addition of duplicate code in multiple catch handlers for the same try block. With JDK 7 comes multi catch handler blocks which can <strong>allow running the same code</strong> when any of the exception classes mentioned in the catch block are executed. Though one could write a method and call it from multiple catch blocks but not all developers were following that and the need was to add some enhancement in the <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/" title="Difference between JVM JRE JDK JIT">JDK</a> to reduce the amount of duplicate exception handling code. The following code shows how to use multi-catch blocks for executing the same code when two or more than two exceptions are raised by a try block. 1 We can use <a href="http://www.javaexperience.com/checked-and-un-checked-exceptions-in-java/" title="Checked and Un-Checked exceptions in Java">checked as well un-checked exceptions</a> in the above catch handler.
Static Analysis in Eclipse using Java
Checked and Un-Checked exceptions in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments