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被覆盖的问题

相关文章:

  • 2021-09-12
  • 2021-08-10
  • 2021-09-17
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2020-04-09
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-10-30
  • 2022-12-23
  • 2022-01-30
  • 2021-09-22
相关资源
相似解决方案