1.1 Try With Resources Enahancements PDF
1.1 Try With Resources Enahancements PDF
1.1 Try With Resources Enahancements PDF
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) }
To overcome these problems Sun People introduced "try with resources" in 1.7 version.
~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) }
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.
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.
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.
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.
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.
Demo Program:
~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:
~6~