1.正常写法 代码:
public class AutoCloseableDemo {
public static void main(String[] args) {
try (AutoCloseableObjecct app = new AutoCloseableObjecct()) {
System.out.println("--try--");
} catch (Exception e) {
System.out.println("--catch--");
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("AutoCloseable.close");
}
}
}
结果:实现AutoCloseable结果,会在执行try之后,finally之前,调用对象的close方法
--try--
AutoCloseable.close
--finally--
2.try中有异常 代码:
import java.io.IOException;
public class AutoCloseableDemo {
public static void main(String[] args) {
try (AutoCloseableObjecct app = new AutoCloseableObjecct()) {
System.out.println("--try--");
throw new IOException();
} catch (Exception e) {
System.out.println("--catch--");
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("AutoCloseable.close");
}
}
}
结果:有异常的情况,还是会自动关闭。调用close是在try 执行之后,catch,finally执行之前。
--try--
AutoCloseable.close
--catch--
--finally--
3.如果 close出现异常,会怎么样,代码:
import java.io.IOException;
public class AutoCloseableDemo {
public static void main(String[] args) {
try (AutoCloseableObjecct app1 = new AutoCloseableObjecct()) {
System.out.println("--try--");
} catch (Exception e) {
System.out.println("--catch--" + e);
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println(" AutoCloseable.close");
throw new IOException("AutoCloseable close Exception");
}
}
}
结果:如果 close出现异常会被catch块捕获到异常。无catch或者catch无捕获到异常(即异常不匹配的),会向外抛出。
--try--
AutoCloseable.close
--catch--java.io.IOException: AutoCloseable close Exception
--finally--
4.多资源情况 代码:
public class AutoCloseableDemo {
public static void main(String[] args) {
try (AutoCloseableObjecct app1 = new AutoCloseableObjecct();
AutoCloseableObjecct app2 = new AutoCloseableObjecct();
AutoCloseableObjecct app3 = new AutoCloseableObjecct()) {
System.out.println("--try--");
} catch (Exception e) {
System.out.println("--catch--");
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println(this + " AutoCloseable.close");
}
}
}
结果:多资源的引用,用;分号隔开
--try--
AutoCloseableDemo$AutoCloseableObjecct@7fbe847c AutoCloseable.close
AutoCloseableDemo$AutoCloseableObjecct@41975e01 AutoCloseable.close
AutoCloseableDemo$AutoCloseableObjecct@c2e1f26 AutoCloseable.close
--finally--
5.多资源close都出现异常,代码:
import java.io.IOException;
public class AutoCloseableDemo {
public static void main(String[] args) {
try (AutoCloseableObjecct app1 = new AutoCloseableObjecct("app1");
AutoCloseableObjecct app2 = new AutoCloseableObjecct("app2");
AutoCloseableObjecct app3 = new AutoCloseableObjecct("app3");) {
System.out.println("--try--");
} catch (Exception e) {
System.out.println("--catch--" + e);
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
String name;
public AutoCloseableObjecct(String name) {
super();
this.name = name;
}
@Override
public void close() throws Exception {
System.out.println(name + " AutoCloseable.close");
throw new IOException(name + " AutoCloseable close Exception");
}
}
}
结果:多资源的都出现异常的情况,close都会执行。close的执行顺序和声明顺序相反。catch捕获到的异常时最先出现异常的close。
--try--
app3 AutoCloseable.close
app2 AutoCloseable.close
app1 AutoCloseable.close
--catch--java.io.IOException: app3 AutoCloseable close Exception
--finally--
6.检验try,catch,finally中 对资源的使用:try块中可以正常使用,catch块和finally块不行
7.检验非try-with-resources 的AutoCloseable实现实例是否会自动关闭:
public class AutoCloseableDemo {
public static void main(String[] args) {
AutoCloseableObjecct app2 = new AutoCloseableObjecct("app2");
try (AutoCloseableObjecct app1 = new AutoCloseableObjecct("app1")) {
System.out.println("--try--");
} catch (Exception e) {
System.out.println("--catch--");
} finally {
System.out.println("--finally--");
}
}
public static class AutoCloseableObjecct implements AutoCloseable {
String name;
public AutoCloseableObjecct(String name) {
super();
this.name = name;
}
@Override
public void close() throws Exception {
System.out.println(name + " AutoCloseable.close");
}
}
}
结果:只有在try-with-resources 中,且实现AutoCloseable,才会自动关闭。
对于实现AutoCloseable的类的实例,可以不放try-with-resources中,从而自己实现关闭资源(在需要自己定制关闭资源的时候)。
--try--
app1 AutoCloseable.close
--finally--
8.Cloasable的源码:实现Cloasable就实现了AutoCloseable,since 1.5,而流基本都实现了Cloasable接口
package java.io;
import java.io.IOException;
/**
* A {@code Closeable} is a source or destination of data that can be closed.
* The close method is invoked to release resources that the object is
* holding (such as open files).
*
* @since 1.5
*/
public interface Closeable extends AutoCloseable {
9.总结