Use try-with-resource elegant closed Resources

  After JDK1.7, the introduction of try-with-resource, so that the closing operation of the resource in a nested without finally, the code number of simple, essentially a syntactic sugar, can be used try-with-resource type resource closed, must implement AutoCloseable interface.

  Prior to version 1.7, the traditional close the resource as follows:

 
 
public static void main(String[] args){

FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("file.txt");
fileInputStream.read();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
assert fileInputStream != null;
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  You can see, in order to ensure that resources are properly closed, you need to re-embed finall finally, try to open more resources, the deeper the finall nesting may lead to close the resource code even more than the business code.

  However, after using the syntax try-with-resource, the above example can be rewritten as:

 
 
try(FileInputStream fileInputStream1 = new FileInputStream("file.txt")){
fileInputStream1.read();
} catch (IOException e) {
e.printStackTrace();
}

  How to interpret whether resources were to be closed, let's handwriting a Demo:

  Realization of resource AutoCloseable

class MyResource implements AutoCloseable{

public void open(){
System.out.println("resource is open!");
}

@Override
public void close() throws Exception {
System.out.println("resource is close!");
}
}

  Caller:

 
 
public static void main(String[] args){

try(MyResource myResource = new MyResource()){
myResource.open();
} catch (Exception e) {
e.printStackTrace();
}
}

  Output as can be seen close method is automatically invoked,

resource is open!
resource is close!

  What is the underlying principle of it, look at the class files compiled:

 
 
try {
MyResource myResource = new MyResource();
Throwable var2 = null;

try {
myResource.open();
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (myResource != null) {
if (var2 != null) {
try {
myResource.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
myResource.close();
}
}

}
} catch (Exception var14) {
var14.printStackTrace () ;
}

}

  Obviously, the compiler generates a finally block, and call the close method, before the realization of the principle of operation of 1.7 close the resource with the same, but you can see, there is more than one call addSuppressed way to do so is in fact to handle the exception mask, what is the exception mask, first of all, let's just change it Demo of the resource class thrown in open and close methods, and the use of resources previously closed 1.7 methods, resources and the caller code changes as follows:

public void open() throws IOException {
System.out.println("resource is open!");
throw new IOException("open() exception!");
}

@Override
public void close() throws Exception {
System.out.println("resource is close!");
throw new IOException("close() exception!");
}
 
 
 
 
public static void main(String[] args) throws Exception {

MyResource myResource = null;

try{
myResource = new MyResource();
myResource.open();
}finally {
try {
myResource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

  Print Console as follows:

     

   Exceptions thrown open method is automatically ignored, and abnormal loss of information will result in program debugging difficult, so try-with-resource syntax added addSuppressed handle exceptions shield, now modify the Demo to use try-with-resource close the resource, call side code is as follows:

 
 
public static void main(String[] args) throws Exception {

try(MyResource myResource = new MyResource()){
myResource.open();
}

  Print Console as follows

  

  Abnormal more information Tip: close method exception is thrown in the open method of inhibiting abnormal thrown.

 

Guess you like

Origin www.cnblogs.com/youtang/p/11441959.html
Recommended