Java 7的try-with-resource语法的目的是提高Java开发人员的效率,使得他们不需要在编写代码时考虑资源释放问题,大多数的这类"清理"问题是由于异常发生时清理方法没有被调用产生。
准备
class OpenException extends Exception{} class SwingException extends Exception{} class CloseException extends Exception{}
构造函数会抛出OpenException,swing()方法会抛出SwingException,close方法会抛出CloseException。
class OpenDoor implements AutoCloseable { public OpenDoor() throws Exception { System.out.println("The door is open."); //throw new OpenException() }; public void swing() throws Exception { System.out.println("The door is becoming unhinged."); //throw new SwingException(); } public void close() throws Exception { System.out.println("The door is closed."); // throw new CloseException();
}
}
public class TryWithResources { public static void main(String[] args) throws Exception { try ( OpenDoor door = new OpenDoor() ) { door.swing(); } catch (Exception e) { System.out.println("Is there a draft? " + e.getClass());//注意e.getClass() } finally { System.out.println("I'm putting a sweater on, regardless. "
);
}
}
}
运行上面的代码,如果没有抛出异常的话,输出应该是这样的:
The door is open. The door is becoming unhinged. The door is closed. I'm putting a sweater on, regardless.
三个异常目前被注释掉了,现在让我们一个个的抛出他们。
开始
- 在'try-with-resource'初始化代码块的异常(构造函数抛出)
如果OpenDoor的构造函数抛出异常时会发生什么?close()方法是否还会被自动调用?让我们来试一下就知道了,去掉构造函数中异常代码的注释:
public OpenDoor() throws Exception { System.out.println("The door is open."); throw new
OpenException();
}
代码打印的结果如下:
The door is open. Is there a draft? class OpenException I'm putting a sweater on, regardless.
可以看到,当构造函数抛出异常时try-with-resource代码体部分的代码没有被执行。当声明资源时,如果有异常抛出,可以认为资源并未正确初始化,所以并需要释放资源。然而,需要注意的时,如果其他资源已被正确初始化,那么还是会按照声明相反的顺序调用那些资源的close()方法。
- try-with-resource代码块中抛出的异常
如果在swing方法中抛出异常,会发生什么呢?
The door is open. The door is becoming unhinged. The door is closed. Is there a draft? class SwingException I'm putting a sweater on, regardless.
从上面的输出我们可以了解到:
- OpenDoor的构造方法被调用了,第一行输出
- OpenDoor的swing方法也被调用了,第二行输出
- 抛出了
SwingException
- close方法被调用,第四行输出
- 异常被catch块捕获,第五行输出
- 执行finally代码块,地六行输出。
只是try-with-resources代码块的标准行为,尽管会使人很困惑:什么时候会执行close方法?规则是:任何AutoCloseable对象的close方法会在任何catch块代码之前被执行。
- 在AutoCloseable对象的close方法中抛出异常
The door is open.
The door is becoming unhinged.
The door is closed.
Is there a draft? class CloseException
I'm putting a sweater on, regardless.
我是天王盖地虎的分割线
参考:http://www.4byte.cn/learning/84919/java-7-xin-te-xing-zi-dong-zi-yuan-guan-li-arm-he-autoclosable-jie-kou-jiao-cheng.html