Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204864

Consider throwing ConnectionException consistently for all connection failures

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 11
    • Fix Version/s: None
    • Component/s: core-libs

      Description


      Consider the following code:

      public class HttpClientTest {
          public static void main(final String[] args) {
              final HttpClient httpClient = HttpClient.newBuilder().build();
              final String targetURL = "http://unknown.host.foo.bar.com.nowhere";
              final HttpRequest request = HttpRequest.newBuilder().uri(URI.create(targetURL))
                                      .method("HEAD", HttpRequest.BodyPublishers.noBody())
                                      .build();
              try {
                  httpClient.send(request, HttpResponse.BodyHandlers.discarding());
              } catch(IOException | InterruptedException e) {
                  // intentionally ignore
                  System.out.println("Caught an intentionally ignored exception");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

      which tries to send a request to (in this case an intentional) non-existent address. The exception that gets thrown is:


      java.nio.channels.UnresolvedAddressException
          at java.base/sun.nio.ch.Net.checkAddress(Net.java:130)
          at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:674)
          at java.net.http/jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$0(PlainHttpConnection.java:113)
          at java.base/java.security.AccessController.doPrivileged(Native Method)
          at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:115)
          at java.net.http/jdk.internal.net.http.Http1Exchange.sendHeadersAsync(Http1Exchange.java:261)
          at java.net.http/jdk.internal.net.http.Exchange.lambda$responseAsyncImpl0$8(Exchange.java:385)
          at java.net.http/jdk.internal.net.http.Exchange.checkFor407(Exchange.java:320)
          at java.net.http/jdk.internal.net.http.Exchange.lambda$responseAsyncImpl0$9(Exchange.java:389)
          at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:930)
          at java.base/java.util.concurrent.CompletableFuture.uniHandleStage(CompletableFuture.java:946)
          at java.base/java.util.concurrent.CompletableFuture.handle(CompletableFuture.java:2266)
          at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl0(Exchange.java:389)
          at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl(Exchange.java:299)
          at java.net.http/jdk.internal.net.http.Exchange.responseAsync(Exchange.java:291)
          at java.net.http/jdk.internal.net.http.MultiExchange.responseAsyncImpl(MultiExchange.java:240)
          at java.net.http/jdk.internal.net.http.MultiExchange.lambda$responseAsync0$1(MultiExchange.java:205)
          at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1072)
          at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
          at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1705)
          at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
          at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
          at java.base/java.lang.Thread.run(Thread.java:832)

      This issue has been filed as a result of a discussion on net-dev:
        http://mail.openjdk.java.net/pipermail/net-dev/2018-June/011537.html

        Attachments

          Activity

            People

            • Assignee:
              chegar Chris Hegarty
              Reporter:
              chegar Chris Hegarty
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: