JEP-353 - Socket.connect now throws NoRouteToHostException as against ConnectException previously
Jaikiran Pai
jai.forums2013 at gmail.com
Sat Aug 21 07:17:19 UTC 2021
JEP-353[1] which got implemented and released in JDK13, states:
"The java.net package defines many sub-classes of SocketException. The
new implementation will attempt to throw the same specific
SocketException as the old implementation but there may be cases where
they are not the same."
In one of the projects I watch, a recent issue[2] shows that the
"Socket.connect(...)" call, in certain cases, now throws a
"java.net.NoRouteToHostException" exception as opposed to
"java.net.ConnectException" in previous versions before this change. The
"Socket.connect(...)" javadoc states that this API can throw an
"IOException", so this change, in theory, is still fine and doesn't
break any API contract. However, as noted in [2], certain libraries
(Apache HTTP client 4.5.x versions in this case) expect a certain
exception type when it's dealing with decision making for HTTP request
retries. Due to this change in the exception type being thrown, the
Apache HTTP client library now behaves differently in Java 11 and Java 16.
Is this change of exception type being thrown intentional? Or is there
interest in changing back to the previous exception type to preserve
backward compatibility? If not, I think the Apache HTTP client library
will have to perhaps do certain changes to have this part of the code
behave the same across Java versions.
For the sake of reference, here's the snippet of the exception
stacktrace in Java 11 and Java 16:
Java 11 stacktrace:
Caused by: java.net.ConnectException: No route to host (connect failed)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at
java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at
java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at
java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:609)
at
org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at
org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 116 more
Java 16 stacktrace:
Caused by: java.net.NoRouteToHostException: No route to host
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:576)
at java.base/sun.nio.ch.Net.connect(Net.java:565)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:645)
at
org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at
org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
[1] https://openjdk.java.net/jeps/353
[2] https://github.com/quarkusio/quarkus/pull/19559
-Jaikiran
More information about the net-dev
mailing list