Java SE 9: Try With Resources Improvements

Posted By : Vipin Pokhriyal | 31-May-2018

The System resources must be closed after when it complete work in your program otherwise it may cause disaster.

Example:File resource, JDBC resource, database connection, Socket connection resource.

Before Java 7 there was not any way resource close itself, we used finally for it there may be chance programmer can forget the close resource so in case it can create the issue.

The way before Java 7 we free the resource the syntax given below:


Free Resource before Java SE 7:

try{
//open resources like File, Database connection, Sockets etc
} catch (FileNotFoundException e) {
// Exception handling here File, DB, Socket etc.
}finally{
// at last finally we close the resources this block must
		execute.
}
 

Java SE 7: Try-With-Resources Basics
 

The syntex of try with resource is given below:

 

try(// open resources here){
// use resources
} catch (FileNotFoundException e) {
// exception handling
}
// resources are closed as soon as try-catch block is executed.

The above syntax of try with resource use java.lang.AutoCloseable Interface. It must Implement AutoCloseable Interface otherwise java compiler will throw compilation error.

Also you can open Multiple Resource with it:

try (BufferedReader br = new BufferedReader(new FileReader("C:\\journaldev.txt"));
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(FileSystems.getDefault().getPath("C:\\journaldev.txt"),
Charset.defaultCharset())) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}

 

Problem occur Try-With-Resources java with 7 & 8:

There was a small problem try with resource, you can see in below code

reader1 is reference of BufferReader class if we want use it with try with resource we should have to use declare a variable or reference for it like the given Below:-
 

void test() throws IOException{
BufferedReader br1 = new BufferedReader(new FileReader("demo.txt"));
try (BufferedReader br2 = br1) {
System.out.println(br2.readLine());
}
}

if we are using br1 directly without declare within try with resource, it will throw compileTime error

void test() throws IOException{
BufferedReader br1 = new BufferedReader(new FileReader("demo.txt"));
try (br1) { //CompileTime Error
System.out.println(br1.readLine());
}
}

Java SE 9: Try-With-Resources Improvements:

Java SE 9 provide improvement the above mention problem, Now it is Not necessary that we have to Need local declaration for try with resource block, In java SE 9 the way it Accept final and effectvily final so we don't need to declare variable.

void test() throws IOException{
BufferedReader br1 = new BufferedReader(new FileReader("demo.txt"));
try (reader1) {
System.out.println(reader1.readLine());
}catch (Exception ex){
//Write Exception handling here
}
}

It work fine into java 9.

 
 
 
 
 
 
 
 
 
 
 
 
 

About Author

Author Image
Vipin Pokhriyal

Vipin is Qualified in Master in computer Applicatins. He is an Active team player & having skills as Java Developer.

Request for Proposal

Name is required

Comment is required

Sending message..