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住后,循环无法继续执行。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM