This method is used to check a URL and return the time it takes to check and the HTTP status code:
public static String urlCheck(String url) {
try {
URL urlToCheck = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlToCheck.openConnection();
connection.setRequestMethod("GET");
Long start = System.currentTimeMillis();
connection.connect();
int urlStatus = connection.getResponseCode();
Long stop = System.currentTimeMillis();
Long urlCheckTime = stop - start;
return (connection.getRequestMethod() + " " + urlToCheck + " "
+ urlStatus + " " + urlCheckTime + "ms");
}
catch (IOException err){
logger.log(Level.WARNING,err.getMessage());
return "Skipping URL";
}
}
My questions are:
- Should my
try
block contain all the lines that are in it now, or should it only contain the line that can throw an exception (I put them all in since variables were not resolving (e.g.urlToCheck
on first line was not passed to the second line if the second line was outside the try block))? - Is using the blanket exception
IOException
ok, or would it be better to create acatch
block for each exception type that thetry
block can throw?