UnknownHostException Java

UnknownHostException Java

Java UnknownHostException

It is a subclass of IOException. This exception is thrown when the IP address of the other system (say, server) is not found by the networking software. No confusion here, between MalformedURLException and UnknownHostException. Wrong format is MalformedURLException and unable to locate in the network it is UnknownHostException.

Following is the hierarchy.

Object –> Throwable –> Exception –> IOException –> UnknownHostException

Full hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions".

Supporting Two constructors

1. UnknownHostException: Creates a UnknownHostException object that can be thrown or handled.
2. UnknownHostException(String message): Creates a UnknownHostException object and the message mentioned in the constructor is displayed to the user.

Observe the signature of Socket constructor.

1. Socket(String serverAddress, int port) throws UnknownHostException, IOException: This constructor (on client-side) connects to server (specified as string as first parameter) and links to the process running on the specified port number passed as second parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *