Resolve a host to every address it has, and handle the checked failure
Resolve a host to every address it has, and handle the checked failure
Answer
try { for (InetAddress a : InetAddress.getAllByName(host)) { System.out.println(a.getHostName() + " -> " + a.getHostAddress()); } } catch (UnknownHostException ex) { System.err.println("no such host: " + host); }
`getAllByName` returns the whole array because one name commonly maps to several addresses; `getByName` would silently pick one of them for you. `UnknownHostException` is checked (it extends `IOException`), so the compiler will not let you forget that a lookup can fail.
Harold 4e ch4 §The InetAddress Class