A straightforward way to check for URL validity, is to simply handle a MalformedURLException
exception:
try {
URL base = new URL(SomeString);
}
catch (MalformedURLException e) {
e.printStackTrace();
// handles this in some way
}
But AFAIK, using exceptions to implement program logic is conceptually incorrect (and perhaps more costly in runtime performance).
On the other hand, I don't know of a Java method that does isValid(String url)
.
Is there a better way to check URL's string validity without instantiating a URL object (and handling a MalformedURLException
)?