JDK7中的新特性 The try-with-resources Statement

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

类似于这样的代码

    try (
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
    ) {
      oos.writeUnshared(attributes);
      oos.flush();
      serialized = bos.toByteArray();
    }

简而言之就是不用自己费事写finally啦, 可以避免finally里面再出exception导致try里的exception被覆盖的问题

posted on 2016-03-14 12:17  Milton  阅读(441)  评论(0编辑  收藏  举报

导航