CloseableResource implements AutoCloseable { public static <T> CloseableResource<T> of(T resource, Closer<T> closer) { checkArgument(resource != null, "Resource must be non-null"); checkArgument(closer != null, "%s must be non-null", Closer.class.getName()); return new CloseableResource<>(resource, closer); } private  CloseableResource(T resource, Closer<T> closer); static CloseableResource<T> of(T resource, Closer<T> closer); T get(); CloseableResource<T> transfer(); @Override void close();  }
@Test public void wrapsExceptionsInCloseException() throws Exception { Exception wrapped = new Exception(); thrown.expect(CloseException.class); thrown.expectCause(is(wrapped)); try (CloseableResource<Foo> ignored = CloseableResource.of( new Foo(), foo -> { throw wrapped; })) { } }