1.1 Try With Resources Enahancements PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Try with Resources Enahancements

Need of Try with Resources:


Until 1.6 version it is highly recommended to write finally block to close all resources
which are open as part of try block.

1) BufferedReader br=null;
2) try
3) {
4) br=new BufferedReader(new FileReader("abc.txt"));
5) //use br based on our requirements
6) }
7) catch(IOException e)
8) {
9) // handling code
10) }
11) finally
12) {
13) if(br != null)
14) br.close();
15) }

Problems in this Approach:


1. Compulsory programmer is required to close all opened resources which increases the
complexity of the programming
2. Compulsory we should write finally block explicitly which increases length of the code
and reduces readability.

To overcome these problems Sun People introduced "try with resources" in 1.7 version.

Try with Resoruces:


The main advantage of "try with resources" is the resources which are opened as part of
try block will be closed automatically Once the control reaches end of the try block either
normally or abnormally and hence we are not required to close explicitly so that the
complexity of programming will be reduced. It is not required to write finally block
explicitly and hence length of the code will be reduced and readability will be improved.

1) try(BufferedReader br=new BufferedReader(new FileReader("abc.txt")))


2) {
3) use be based on our requirement, br will be closed automatically , Onec control reaches
end of try either normally or abnormally and we are not required to close explicitly

~1~
4) }
5) catch(IOException e)
6) {
7) // handling code
8) }

Conclusions:
1. We can declare any no of resources but all these resources should be seperated with
;(semicolon)

1) try(R1 ; R2 ; R3)
2) {
3) -------------
4) }

2. All resources should be AutoCloseable resources. A resource is said to be auto closable


if and only if the corresponding class implements the java.lang.AutoCloseable interface
either directly or indirectly.

All database related, network related and file io related resources already implemented
AutoCloseable interface. Being a programmer we should aware this point and we are not
required to do anything extra.

3. AutoCloseable interface introduced in Java 1.7 Version and it contains only one
method: close()

4. All resource reference variables should be final or effectively final and hence we can't
perform reassignment within the try block.

1) try(BufferedReader br=new BufferedReader(new FileReader("abc.txt")))


2) {
3) br=new BufferedReader(new FileReader("abc.txt"));
4) }

CE : Can't reassign a value to final variable br

5. Untill 1.6 version try should be followed by either catch or finally but in 1.7 version we
can take only try with resource without catch or finally

1) try(R)
2) {
3) //valid
4) }

~2~
6.The main advantage of "try with resources" is finally block will become dummy because
we are not required to close resources of explicitly.

Problems with JDK 7 Try with Resources:


1. The resource reference variables which are created outside of try block cannot be used
direclty in try with resources.

1) BufferedReader br=new BufferedReader(new FileReader("abc.txt"))


2) try(br)
3) {
4) // Risky code
5) }

This syntax is invalid in until java 1.8V.

We should create the resource in try block primary list or we should declared with new
reference variable in try block. i.e Resource reference variable should be local to try block.

Solution-1: Creation of Resource in try block primary list


1) try(BufferedReader br=new BufferedReader(new FileReader("abc.txt")))
2) {
3) // It is valid syntax in java 1.8V
4) }

Solution-2: Assign resource with new reference variable


1) BufferedReader br=new BufferedReader(new FileReader("abc.txt"))
2) try(BufferedReader br1=br)
3) {
4) // It is valid syntax in java 1.8V
5) }

But from JDK 9 onwards we can use the resource reference variables which are created
outside of try block directly in try block resources list.i.e The resource reference variables
need not be local to try block.

1) BufferedReader br=new BufferedReader(new FileReader("abc.txt"))


2) try(br)
3) {
4) // It is valid in JDK 9 but in valid until JDK 1.8V
5) }

But make sure resource(br) should be either final or effectively final. Effectively final
means we should not perform reassignment.

~3~
This enhancement reduces length of the code and increases readability.

1) MyResource r1 = new MyResource();


2) MyResource r2 = new MyResource();
3) MyResource r3 = new MyResource();
4) try(r1,r2,r3)
5) {
6) }

Demo Program:

1) class MyResource implements AutoCloseable


2) {
3) MyResource()
4) {
5) System.out.println("Resource Creation...");
6) }
7) public void doProcess()
8) {
9) System.out.println("Resource Processing...");
10)
11) }
12) public void close()
13) {
14) System.out.println("Resource Closing...");
15) }
16) }
17) class Test
18) {
19) public static void preJDK7()
20) {
21) MyResource r=null;
22) try
23) {
24) r=new MyResource();
25) r.doProcess();
26) }
27) catch (Exception e)
28) {
29) System.out.println("Handling:"+e);
30) }
31) finally
32) {
33) try
34) {

~4~
35) if (r!=null)
36) {
37) r.close();
38) }
39) }
40) catch (Exception e)
41) {
42) System.out.println("Handling:"+e);
43) }
44) }
45) }
46) public static void JDK7()
47) {
48) try(MyResource r=new MyResource())
49) {
50) r.doProcess();
51) }
52) catch(Exception e)
53) {
54) System.out.println("Handling:"+e);
55) }
56) }
57) public static void JDK9()
58) {
59) MyResource r= new MyResource();
60) try(r)
61) {
62) r.doProcess();
63) }
64) catch(Exception e)
65) {
66) System.out.println("Handling:"+e);
67) }
68) }
69) public static void multipleJDK9()
70) {
71) MyResource r1= new MyResource();
72) MyResource r2= new MyResource();
73) MyResource r3= new MyResource();
74) MyResource r4= new MyResource();
75) try(r1;r2;r3;r4)
76) {
77) r1.doProcess();
78) r2.doProcess();
79) r3.doProcess();
80) r4.doProcess();
81) }

~5~
82) catch(Exception e)
83) {
84) System.out.println("Handling:"+e);
85) }
86) }
87) public static void main(String[] args)
88) {
89) System.out.println("Program Execution With PreJDK7");
90) preJDK7();
91)
92) System.out.println("Program Execution With JDK7");
93) JDK7();
94)
95) System.out.println("Program Execution With JDK9");
96) JDK9();
97) System.out.println("Program Execution Multiple Resources With JDK9");
98) multipleJDK9();
99) }
100) }

Output:

Program Execution With PreJDK7


Resource Creation...
Resource Processing...
Resource Closing...
Program Execution With JDK7
Resource Creation...
Resource Processing...
Resource Closing...
Program Execution With JDK9
Resource Creation...
Resource Processing...
Resource Closing...
Program Execution Multiple Resources With JDK9
Resource Creation...
Resource Creation...
Resource Creation...
Resource Creation...
Resource Processing...
Resource Processing...
Resource Processing...
Resource Processing...
Resource Closing...
Resource Closing...
Resource Closing...
Resource Closing...

~6~

You might also like