Kin*_*lan 1 java connection exception-handling exception
你好我们试图通过for循环打印字符串数组中的一组url中的标题,而打印时,在某些情况下无法建立连接并引发异常,这会阻止所有剩余的元素....
我正在使用此代码进行打印,有人可以帮助我忽略异常并继续使用字符串数组中的另一个元素循环...
for (int f = 0; f < fin.length; f++) //fin be string array of urls
{
Document finaldoc = Jsoup.connect(fin[f]).get();
out.println(finaldoc.title());
}
Run Code Online (Sandbox Code Playgroud)
其中一个例外是
java.net.SocketTimeoutException: Read timed out
Run Code Online (Sandbox Code Playgroud)
Igo*_*gor 14
你需要一个 try{ } catch { }
for (int f = 0; f < fin.length; f++) //fin be string array of urls
{
try {
Document finaldoc = Jsoup.connect(fin[f]).get();
out.println(finaldoc.title());
}
catch (SocketTimeoutException exception) {
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
显然这不是一个好的做法,因为它不能解决甚至处理错误,但这是你继续迭代循环的方法.