java的for循环中遇到异常抛出后继续循环执行

在for循环中调用https时,如果一个https请求超时,如何保证继续下一个呢?

for (District district : alldistrict) {
try {
System.out.println("=======机构:" + district + "查询========");
//查询所有的区县
JSONObject jsonObject = productsClient.queryDuns(district.getAREACODE());

if (jsonObject.getInteger("errorCode") == -1) {
return result.error500(jsonObject.getString("errorInfo"));
}
JSONArray aa = jsonObject.getJSONArray("operateFileItem");
List<Map> list = JSONObject.parseArray(aa.toJSONString(), Map.class);
System.out.println(list);
organizationService.saveAllOrganization(list);
} catch (Exception e) {
System.out.println("异常跳出" + e);
//continue;--- 不需要写continue,因为写不写,都会继续循环,不会异常后直接退出的。
}
}

在循环中添加try catch
如果try包在for循环外面,则无法达到预期效果,遇到异常抛出,被catch住后,循环无法继续执行。

纵有白头俱老意,奈何缘浅路芊芊.
原文地址:https://www.cnblogs.com/hanby/p/15620730.html