The code has to be elegant
Shutdown position of resource before JDK7:
/** * public class CloseResourceBefore7 {private static final String FileName ="file.txt";
public static void main(String[] args) throws IOException {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(FileName);
char c1 = (char) inputStream.read();
System.out.println("c1=" + c1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(inputStream ! = null) { inputStream.close(); }}}}Copy the code
{try-with-resource}} {try-with-resource} All objects that implement the java.lang.AutoCloseable interface (which includes all objects that implement java.io.Closeable) can be used as resources. The Resource class that implements the java.lang.AutoCloseable interface:
/** * @author hetiantian ** / public class Resource implements AutoCloseable {public void implements AutoCloseablesayHello() {
System.out.println("hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource is closed"); }}Copy the code
The test class CloseResourceIn7. Java
** @author hetiantian ** / public class CloseResourceIn7 {public static void main(String[] args) { try(Resource resource = new Resource()) { resource.sayHello(); } catch (Exception e) { e.printStackTrace(); }}}Copy the code
Print result:
hello
Resource is closed
Copy the code
If there are more than one open resource: resource two resource2.java
/** * @author hetiantian ** / public class Resource2 implements AutoCloseable {public voidsayhello() {
System.out.println("Resource say hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource2 is closed"); }}Copy the code
The test class CloseResourceIn7. Java
** @author hetiantian ** / public class CloseResourceIn7 {public static void main(String[] args) { try(Resource resource = new Resource(); Resource2 resource2 = new Resource2()) { resource.sayHello(); resource2.sayhello(); } catch (Exception e) { e.printStackTrace(); }}}Copy the code
Print result:
hello
Resource say hello
Resource2 is closed
Resource is closed
Copy the code
Even if the resources are large, the code can be written very succinctly. If you close the resources in the same way as before JDK7, the more resources there are, the more nesting there is when you close the resources with Fianl
Closeresourcein7.class = closeresourcein7.class = closeresourcein7.class = closeresourcein7.class
public class CloseResourceIn7 {
public CloseResourceIn7() {
}
public static void main(String[] args) {
try {
Resource resource = new Resource();
Throwable var2 = null;
try {
resource.sayHello();
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if(resource ! = null) {if (var2 != null) {
try {
resource.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else{ resource.close(); } } } } catch (Exception var14) { var14.printStackTrace(); }}}Copy the code
You can see that var2.addsuppressed (var11) from the try-catch-finally statement block finally was generated after compilation; 🤔️ is actually doing this to handle exception masking. Let’s change the code to Resource. Java
/** * @author hetiantian ** / public class Resource implements AutoCloseable {public void sayHello() throws Exception { throw new Exception("Resource throw Exception");
}
@Override
public void close() throws Exception {
throw new Exception("Close method throw Exception"); }}Copy the code
Both methods throw exceptions
The test class CloseResourceIn7. Java
** @author hetiantian ** / public class CloseResourceIn7 {public static void main(String[] args) { try { errorTest(); } catch (Exception e) { e.printStackTrace(); } } private static void errorTest() throws Exception { Resource resource = null; try { resource = new Resource(); resource.sayHello(); } finally {if(resource ! = null) { resource.close(); }}}}Copy the code
Print result:
java.lang.Exception: Close method throw Exception
at com.shuwen.Resource.close(Resource.java:15)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:27)
at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
Copy the code
Closeresourcein7.java is a try-with-resource method that is used to implement closeresourcein7.java
** @author hetiantian ** / public class CloseResourceIn7 {public static void main(String[] args) { try { errorTest(); } catch (Exception e) { e.printStackTrace(); } } private static void errorTest() throws Exception { try(Resource resource = new Resource()) { resource.sayHello(); }}}Copy the code
Print information:
java.lang.Exception: Resource throw Exception
at com.shuwen.Resource.sayHello(Resource.java:10)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:20)
at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
Suppressed: java.lang.Exception: Close method throw Exception
at com.shuwen.Resource.close(Resource.java:15)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:21)
... 1 more
Copy the code
You can see that there is a hint of Suppressed in the Exception information, telling us that this Exception actually consists of two exceptions. This Exception is the one that was Suppressed [Suppressed]
How about the summary? Is it easy, if you learn it