In addition to the fact that the isConnected check is probably wrong (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52135), if the socket is really not connected, this should not result in a NotYetConnectedException for java.net. NotYetConnectedException was introduced with java.nio and is an unchecked exception, and thus probably not expected by code using java.net. A SocketException should be used in this case instead.