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