title: Move resource out of try-with
body: <p>I want to create a class instance which wraps a resource. The problem: when the constructor throws, the resource is lost. I'm trying to find a solution for that. Try-with-resource is a construct that seems good for that, but I can not move the resource out of it.</p>
  <p>For example, a service client that wraps a HTTP client:</p>
  <pre><code>class ServiceClient implements Closeable {
    ServiceClient(ClosableHTTPClient client) { /* ... */ }
    public close() { client.close() }
  
    public ServiceClient create(String url) throws IOException {
      try (ClosableHTTPClient client = createHttpClient(url)) {
        return new ServiceClient(client);
      }  // make try-with do not close `client` on success
    }
  
    public ClosableHTTPClient createHttpClient(String url) {
      return HttpClientBuilder.create()
          .setConnectionManager(createClosableConnectionManager()) // must be closed, when `build` throws 
          .build();
    }
  }
  </code></pre>