This is my below code and I want to catch the exception if any IPV6 address is passed. Any idea how it can be done in my code?
private static boolean validateAnIpAddress(String ipAddr) {
InetAddress address = null;
boolean isIPv4;
try {
address = InetAddress.getByName(ipAddr);
isIPv4 = address.getHostAddress().equals(ipAddr) && address instanceof Inet4Address;
} catch (UnknownHostException e) {
getLogger().log(LogLevel.ERROR, e.getMessage());
//return false;
isIPv4 = false;
}
return isIPv4;
}
By checking if
addressis anInet6Addresstype, you can throw the exception inside thetryblock and therefore triggering yourcatchblock.