1

I want to throw an exception that when a user enters an invalid IP address, host name or not a fully qualified domain name it'll bring up an error message.

I'm not too sure whether to use unknownhostexception or IOException.

I tried doing if statement but I don't know what 'invalid' can be in java.

If (addr != ' not a valid IP address, host name, fully qualified domain name or entered something invalid ')
{
throw new IOException/UnknownHostException("this is invalid: " + addr); }

Can anyone help please? Thanks in advance.

Ajay Punja
  • 101
  • 4
  • 11

2 Answers2

2

Try InetAddress.getByName(str) to validate the string. It will throw an UnknownHostException if necessary. I suggest removing your if statement entirely. Perhaps something like this:

public static InetAddress testAddress(String str) throws UnknownHostException {
    InetAddress add = InetAddress.getByName(str);

    // Check if IP address was simply returned, instead of host.
    if (add.getCanonicalHostName().equals(add.getHostAddress())) {
        throw new UnknownHostException(str + "is not a known host.");
    }
    return add;
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • I did this 'InetAddress.getByName(str)' above my if statement. I don't know the part after the '!=' bit. – Ajay Punja Mar 07 '13 at 19:27
  • @AjayPunja I don't see the need for the `if` statement. If the IP/host is invalid, `InetAddress.getByName()` will throw an exception. If no exception is thrown, it passes. Just remove your `if` statement. – martinez314 Mar 07 '13 at 19:32
  • OK so all I need is ' throws MalformedURLException { //code...etc} ' and it should throw an exception when I enter an invalid address. Say, instead of www.stackoverflow.com, I enter www.staccccckoveerflow.ocm and it should bring an error? – Ajay Punja Mar 07 '13 at 19:38
  • @AjayPunja See my revised example. – martinez314 Mar 07 '13 at 19:43
  • I tested it out with Unknownhostexception but it returns 81.200.64.50, instead of an error. I entered a false IP but I'm not too sure why it doesn't return error and returns an address. – Ajay Punja Mar 07 '13 at 19:43
  • i tried your code but it doesn't return any error messages. this is really bugging me :S – Ajay Punja Mar 07 '13 at 19:50
  • @AjayPunja See revised example. – martinez314 Mar 07 '13 at 20:03
  • The return statement is incompatible because add isn't a String. – Ajay Punja Mar 07 '13 at 22:22
  • I managed to solve the string part but it when I entered "www.waa.cat" it returns me an IP address and not an exception. I tried this with many wrong URL's, but still returns same IP address instead of exception. Any clue why? – Ajay Punja Mar 07 '13 at 22:41
0

There is a concept, called regex (regular expression) where you can check if that pattern is correct. You may have to search for good solutions or write your own regex (which is not so easy in my personal opinion). But here is a good starting point http://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ ;)

JumbleGee
  • 85
  • 1
  • 9