I trying to check if an URL which I want to connect to exists or not. Here's my attempt:
try {
// Connect to the url
document = Jsoup.connect("http://www.malformedurl.com").get();
tags = document.select(".tags .tag a");
num = document.select(".tag .count");
// Take the wanted data
UrlFunctions.UrlParse(tags, num);
} catch (java.net.MalformedURLException e) {
System.out.println("URL DOESNT EXIST");
}
After running that, I don't get the message URL DOESNT EXIST
. What exception should I use or what else should I do?
A
MalFormedURLException
will only be thrown when the URL is really malformed, i.e. it does not conform the URL spec, not when it does not exist. This is under the covers been thrown by the constructor of thejava.net.URL
class. Its javadoc tells the following:So, it will only be thrown when you use for example
"www.malformedurl.com"
or"foo://www.malformedurl.com"
instead of"http://www.malformedurl.com"
.To detect whether an URL exists you'd better to head for a different solution. If the host name is unknown, then you should catch
UnknownHostException
instead:This is not necessarily a problem of the other end, it can also occur when the DNS server on your network is bogus.
Or, to detect whether an IP address is reachable, then you should catch
SocketTimeoutException
instead: