循环中出现异常时记录异常并继续执行循环
for(AdminDataContent adminDataContent:adminDataContentList){ try { String tableName=adminDataContent.getTableName(); String sql = "describe " +tableName ; PreparedStatement ps = conn.prepareStatement(sql); ResultSet resultset = ps.executeQuery(); while(resultset.next()){ AdminDataContentDetail contentDetail=new AdminDataContentDetail(); contentDetail.setId(new UUIDUtils().getUUID()); contentDetail.setContentId(adminDataContent.getId()); contentDetail.setDatasourceId(adminDataContent.getDataSourceId()); contentDetail.setColumnName(resultset.getString(1)); contentDetail.setColumnType(resultset.getString(2)); contentDetail.setCreateBy(adminDataContent.getCreateBy()); contentDetail.setCreateDate(new Date()); contentDetail.setUpdateBy(adminDataContent.getUpdateBy()); contentDetail.setUpdateDate(new Date()); contentDetail.setDelFlag("0"); contentDetailsList.add(contentDetail); } } catch (Exception e) { e.printStackTrace(); TableScanError error=new TableScanError(); error.setId(new UUIDUtils().getUUID()); error.setDataSourceId(adminDataContent.getDataSourceId()); error.setErrorTableName(adminDataContent.getTableName()); error.setErrorDatabase(StringUtils.substringBefore(adminDataContent.getTableName(), ".")); //error.setCreateBy(adminDataContent.getCreateBy()); error.setCreateDate(new Date()); //error.setUpdateBy(adminDataContent.getUpdateBy()); error.setUpdateDate(new Date()); error.setRemarks(e.toString()); errors.add(error); continue; } }
实例如上,在循环中使用try catch,catch中进行异常处理并使用continue进入下一次循环