In this article, the enhancements made by Oracle in version 1.7, along with its implementation, has been discussed.
As a part of 1.7 version enhancement, in Exception Handling, the following two concepts have been introduced by Oracle:
- Try with resources.
- Multiple catch blocks.
Try with resources
Until version 1.6, it was highly recommended to write finally block to close all the resources which are open as a part of the try block.
For example:
import java.io.*;
import java.util.*;
class Resource {
public static void main(String args[])
{
BufferedReader br = null ;
String str = " " ;
System.out.println(
"Enter the file path" );
br
= new BufferedReader(
new InputStreamReader(
System.in));
try {
str = br.readLine();
String s;
br
= new BufferedReader(
new FileReader(str));
while ((s = br.readLine()) != null ) {
System.out.println(s);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (br != null )
br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
|
Output:
hello
java
Advantage of try with recourses:
- The main advantage of try with resources is that the resources which were opened as the part of try block will be closed automatically once the control reaches the end of the try block either normally or abnormally. Hence, we are not required to close it explicitly. Therefore, the complexity of programming will be reduced.
- We can declare any number of resources but all these resources should be separated with semicolon(;)
try(R1;R2;R3)
{
- - - - - - -
- - - - - - -
}
- All the resources should be AutoCloseable resources. A resource is said to be auto closeable if and only if the corresponding class
implements the java.lang.AutoCloseable interface either directly or indirectly.
For example, all the database related, network-related and file IO related resources already implemented AutoCloseable interface.
- All resources reference variables are implicitly final and hence we can’t perform reassignment within the try block. For example, in the code below, we are reassigning the value of br for which we get a compile-time error.
try (BufferedReader br = new BufferedReader( new FileReader( "abc.txt" ))) {
br = new BufferedReader( new FileReader( "abc.txt" ));
}
|
- Until version 1.6, try should be followed by either catch or finally but 1.7 version we can take only try with resources without a catch or finally statement.
try(R)
{ //valid
}
Below is the implementation of the enhanced try-catch block:
try (bufferedReader br = new BufferedReader( new FileReader( "abc.txt" ))) {
}
catch (IOException e) {
|
Multiple catch blocks:
Until version 1.6, if we wish to handle multiple exceptions for the same try block, we had to define all the exceptions in multiple catch statements.
For example:
try {
-------- - -------- -
}
catch (ArithmeticException e) {
e.printStackTrace();
}
catch (e) {
e.printStackTrace();
}
catch (ClassCastException e) {
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
|
To overcome this problem, the “Multi catch block” concept is introduced in 1.7 version. Therefore, all the exceptions can be handled with a single catch statement.
Below is the implementation of the multi-catch statement:
try {
------ - ------ -
}
catch (ArithmeticException | NullpointerException
| ClassCastException | IOException e) {
System.out.println(e.getMessage());
}
|
Note: In multi-catch block, there should not be any relation between Exception types (either child to parent or parent to child or the same type.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
04 Apr, 2020
Like Article
Save Article