try-with-resources语句是一种声明了一种或多种资源的try语句。资源是指在程序用完了之后必须要关闭的对象。try-with-resources语句保证了每个声明了的资源在语句结束的时候都会被关闭。任何实现了java.lang.AutoCloseable接口的对象,和实现了java.io.Closeable接口的对象,都可以当做资源使用。
下面是使用样例
1.申明两个实现AutoCloseable接口的类
package jdk7_new_character;public class AutoCloseableTestClass implements AutoCloseable { @Override public void close() throws Exception { System.out.println("autoclose AutoCloseableTestClass"); }}
package jdk7_new_character;public class AutoCloseableTestClass2 implements AutoCloseable { @Override public void close() throws Exception { System.out.println("autoclose AutoCloseableTestClass2"); }}
2.测试
package jdk7_new_character;public class Test { @org.junit.Test public void test() { // fail("Not yet implemented"); } @org.junit.Test public void testAutoClose() { System.out.println("begin testAutoClose"); /* * try { AutoCloseableTestClass autoCloseableTestClass = new * AutoCloseableTestClass(); } catch (Exception e) { } finally { * * } */ try (AutoCloseableTestClass autoCloseableTestClass = new AutoCloseableTestClass(); AutoCloseableTestClass2 autoCloseableTestClass2 = new AutoCloseableTestClass2() ) { System.out.println("try{}"); } catch (Exception e) { e.printStackTrace(); } System.out.println("over testAutoClose"); }}
3.运行结果
可以看到你可以在一个try-with-resources语句里面声明多个资源。当代码块中代码终止,不管是正常还是异常,对象的close方法都会自动按声明的相反顺序调用。
注意:try-with-resources语句也可以像普通的try语句一样,有catch和finally代码块。在try-with-resources语句中,任何的catch和finally代码块都在所有被声明的资源被关闭后执行。
被压抑的异常
try-with-resources语句相关联的代码块可能会抛出异常。try代码块中可能会抛出异常,并且有高达两个异常可能会在try-with-resources语句抛出。如果在try代码块中抛出一个异常,同时在try-with-resources语句中抛出一个或多个异常,那么在try-with-resources语句中抛出的异常就被压抑了,并且最终在方法抛出的异常就是try代码块中抛出的那个异常。你可以通过被try代码块抛出的异常的Throwable.getSuppressed方法找回被压抑的异常。
下面是测试
1.被压抑的异常
申明两个实现AutoCloseable接口的class,并在close方法中抛出异常
package jdk7_new_character;/** * @author 施俊杰 */public class AutoCloseableThrowableTestClass implements AutoCloseable { @Override public void close() throws Exception { System.out.println("close AutoCloseableThrowableTestClass"); throw new Exception("AutoCloseableThrowableTestClass exception"); }}
package jdk7_new_character;/** * @author 施俊杰 */public class AutoCloseableThrowableTestClass2 implements AutoCloseable { @Override public void close() throws Exception { System.out.println("close AutoCloseableThrowableTestClass2"); throw new Exception("AutoCloseableThrowableTestClass2 exception"); }}
创建测试class去创建上面申明的两个类
package jdk7_new_character;public class ThrowableTestClass { public String testThrowable() throws Exception { try (AutoCloseableThrowableTestClass autoCloseableThrowableTestClass = new AutoCloseableThrowableTestClass(); AutoCloseableThrowableTestClass2 autoCloseableThrowableTestClass2 = new AutoCloseableThrowableTestClass2()) { throw new Exception("exception3"); } } public static void main(String[] args) { try { new ThrowableTestClass().testThrowable(); } catch (Exception e) { System.out.println(e.getMessage()); } }}
运行结果
可以看到
AutoCloseableThrowableTestClass2 exception
AutoCloseableThrowableTestClass exception两个exception被压抑了,我们可以通过Throwable.getSuppressed方法找回被压抑的异常,示例代码如下
package jdk7_new_character;public class ThrowableTestClass { public String testThrowable() throws Exception { try (AutoCloseableThrowableTestClass autoCloseableThrowableTestClass = new AutoCloseableThrowableTestClass(); AutoCloseableThrowableTestClass2 autoCloseableThrowableTestClass2 = new AutoCloseableThrowableTestClass2()) { throw new Exception("exception3"); } } public static void main(String[] args) { try { new ThrowableTestClass().testThrowable(); } catch (Exception e) { System.out.println(e.getMessage()); Throwable[] suppressed = e.getSuppressed(); for (Throwable throwable : suppressed) { System.out.println(throwable.getMessage()); } } }}
运行结果
本文属于原创,如有转载请注明出处