Java Try Catch Final-Final在变量中始终为null
我目前在代码方面遇到了一个问题,我不明白为什么这条语句的计算结果是这样的。这是我第一次使用finally块,所以可能有一些基本行为我还不了解 此方法的作用是从api获取json文档,并将所述文档存储为Java Try Catch Final-Final在变量中始终为null,java,null,try-catch,Java,Null,Try Catch,我目前在代码方面遇到了一个问题,我不明白为什么这条语句的计算结果是这样的。这是我第一次使用finally块,所以可能有一些基本行为我还不了解 此方法的作用是从api获取json文档,并将所述文档存储为this.thisPage。然后另一个方法sliceItem将结果字段拆分为json对象数组 每当API返回包含坏字段的json时(例如,字符串字段存储为int,或int存储为double等),就会引发格式错误的json异常。这将尝试10次(由failsafeget处理),如果失败10次,将抛出Ma
this.thisPage
。然后另一个方法sliceItem
将结果字段拆分为json对象数组
每当API返回包含坏字段的json时(例如,字符串字段存储为int,或int存储为double等),就会引发格式错误的json异常。这将尝试10次(由failsafeget处理),如果失败10次,将抛出MalformedJsonException(RuntimeException)。在这种情况下,我希望slicePage做的是获取下一页,而不是继续此页。为了简化这一点,每页有100个条目;如果偏移量3500被破坏,我们希望得到偏移量3600
我目前面临的问题是,resp
在最后一个块中的计算结果总是null
。我无法理解为什么会出现这种情况,因为try块可以返回null以外的内容(JSONObject类型)
任何帮助都将不胜感激,如果您需要更多信息/代码,我愿意提供
public synchronized void slicePage(){
JSONObject resp=null; // otherwise java complains that not initialised
ApiClient apiClient = new ApiClient();
RestEndPoint pageUrl;
while (true) {
pageUrl = getNextPageEndPoint();
if(pageUrl == null) {
throw new IllegalStateException("We have reached the end and the code isn't designed to handle the end here"); // we have reached the end
}
currentPageNumber++;
try {
resp = apiClient.failSafeGet(pageUrl, getRetryCount());
break;
}
catch (MalformedJsonException e) {
logger.info(String.format("The json was still broken after %d retries. Skipping this page and notifying listeners", getRetryCount()));
for (Consumer<Integer> consumer: onSkipListenerList) {
consumer.accept(batchSize); // inform each listener that we are skipping this many entries
}
}
finally { // We need to set the next page end point no matter the outcome of the try catch. N.B. this gets executed even if there is a break
if(resp == null) {
// no next possible
setNextPageEndPoint(null); // don't consider next; we reached the max
this.thisPage = null;
} else {
if(currentPageNumber > maxPages - 1) {
// because a request has been made already, so reduce by 1
setNextPageEndPoint(null); // don't consider next; we reached the max
} else {
// else consider next page
setNextPageEndPoint(constructNextPageEndPoint(pageUrl, resp));
}
this.thisPage = this.parseResult(resp);
setTotalCount(resp.getInt("totalResults"));
}
}
}
}
编辑2
事实证明,代码分析是错误的,一旦运行,该值就不为null。感谢大家(包括评论员)分享您的见解和建议。最后,我插入了一个logger.info,其中包含条件之前的值,一切似乎都正常。它似乎停止工作的原因是图形服务器正在超时。这是正常行为。这个电话
apiClient.failSafeGet(pageUrl, getRetryCount());
引发异常,因此对resp
的值赋值永远不会完成,因此在finally块中
的值为null。因此,要么您的方法总是抛出异常,要么,如果不是,它在某个点返回null
。在代码中:
try {
resp = apiClient.failSafeGet(pageUrl, getRetryCount());
break;
}
catch (MalformedJsonException e) {
logger.info(String.format("The json was still broken after %d retries. Skipping this page and notifying listeners", getRetryCount()));
for (Consumer<Integer> consumer: onSkipListenerList) {
consumer.accept(batchSize); // inform each listener that we are skipping this many entries
}
}
finally {.....
试试看{
resp=apiClient.failSafeGet(pageUrl,getRetryCount());
打破
}
捕获(格式不正确的异常){
logger.info(String.format(“json在%d次重试后仍然被破坏。跳过此页面并通知侦听器”,getRetryCount());
用于(消费者:onSkipListenerList){
consumer.accept(batchSize);//通知每个侦听器我们正在跳过这么多条目
}
}
最后{。。。。。
如果resp=apiClient.failSafeGet(pageUrl,getRetryCount())抛出异常,resp将始终为null,因为程序在将实例分配给resp之前失败。如果
resp
始终为null,则表明failSafeGet
总是抛出异常,或者它返回null
。是否已隔离发生了哪些异常?是否已跳过调试器中的代码是什么?当我提到它总是null
时,我不清楚-IDE说它的计算结果总是为null,但我在过去几个月一直使用这种方法。代码的逻辑中有一个缺陷。谢谢大家的回答“IDE说它总是计算结果为null”到底是什么意思?哪个IDE?您可以编写一个简短但完整的程序来显示相同的行为吗?通过在控制台中打印resp变量进行检查,验证它是否为空
try {
resp = apiClient.failSafeGet(pageUrl, getRetryCount());
break;
}
catch (MalformedJsonException e) {
logger.info(String.format("The json was still broken after %d retries. Skipping this page and notifying listeners", getRetryCount()));
for (Consumer<Integer> consumer: onSkipListenerList) {
consumer.accept(batchSize); // inform each listener that we are skipping this many entries
}
}
finally {.....