Java Reference
In-Depth Information
UnknownHostException if the host name is not recognised, we must either throw this
exception or (preferably) handle it with a catch clause. The following example
illustrates this.
Example
import java.net.*;
import java.util.*;
public class IPFinder
{
public static void main(String[] args)
{
String host;
Scanner input = new Scanner(System.in);
InetAddress address;
System.out.print("\n\nEnter host name: ");
host = input.next();
try
{
address = InetAddress.getByName(host);
System.out.println("IP address: "
+ address.toString());
}
catch (UnknownHostException uhEx)
{
System.out.println("Could not fi nd " + host);
}
}
}
The output from a test run of this program is shown in Fig. 2.1.
Search WWH ::




Custom Search