Java中catch块中return的奇怪问题
我使用的 try/catch 块遇到了一个奇怪的问题。我有这个方法,它只是从远程服务获取一些数据并存储它。
public WFSGetCapabilitiesResponse wfsGetCapabilities(String url) {
WFSGetCapabilitiesResponse response = new WFSGetCapabilitiesResponse();
try {
WFSDataStore data = loadWFSCapabilities(url);
String[] typeNames = data.getTypeNames();
ArrayList<WFSFeatureTypeBase> wfsLayers = new ArrayList<WFSFeatureTypeBase>();
for (int i = 0; i < typeNames.length; i++) {
String typeName = typeNames[i];
WFSFeatureTypeBase newLayer = new WFSFeatureTypeBase();
newLayer.setTypeName(typeName);
newLayer.setName(typeName.split(":")[1]);
newLayer.setTitle(data.getFeatureTypeTitle(typeName));
newLayer.setAbstract(data.getFeatureTypeAbstract(typeName));
newLayer.setServiceUrl(url.split("\\?")[0]);
wfsLayers.add(newLayer);
}
response.setWFSLayers(wfsLayers);
} catch (IOException e) {
response.setError(WCSCapabilitiesResponse.IO_EXCEPTION);
response.setErrorMessage(e.getMessage());
response.setSuccessful(false);
e.printStackTrace();
return response;
}
return response;
}
如果我运行这段代码,我会得到一个空指针异常(“数据”为空,但并不真正知道它是否相关)。但是,如果我从 catch 块中删除 return 语句,一切都很好。
奇怪的是,这两种情况都没有捕获 IOException,所以我不明白为什么它会产生如此大的影响。
再说一次,在 catch 块中返回它不起作用,没有它它就起作用......我真的不明白为什么会发生这种情况。
有什么想法吗?
I've got a strange problem with a try/catch block I'm using. I've got this method, which just gets some data from a remote service and stores it.
public WFSGetCapabilitiesResponse wfsGetCapabilities(String url) {
WFSGetCapabilitiesResponse response = new WFSGetCapabilitiesResponse();
try {
WFSDataStore data = loadWFSCapabilities(url);
String[] typeNames = data.getTypeNames();
ArrayList<WFSFeatureTypeBase> wfsLayers = new ArrayList<WFSFeatureTypeBase>();
for (int i = 0; i < typeNames.length; i++) {
String typeName = typeNames[i];
WFSFeatureTypeBase newLayer = new WFSFeatureTypeBase();
newLayer.setTypeName(typeName);
newLayer.setName(typeName.split(":")[1]);
newLayer.setTitle(data.getFeatureTypeTitle(typeName));
newLayer.setAbstract(data.getFeatureTypeAbstract(typeName));
newLayer.setServiceUrl(url.split("\\?")[0]);
wfsLayers.add(newLayer);
}
response.setWFSLayers(wfsLayers);
} catch (IOException e) {
response.setError(WCSCapabilitiesResponse.IO_EXCEPTION);
response.setErrorMessage(e.getMessage());
response.setSuccessful(false);
e.printStackTrace();
return response;
}
return response;
}
If I run with this code, I get a null pointer exception ('data' is null, but don't really know if it's relevant). However, if I remove the return statement from my catch block everything is fine.
The strange thing is, the IOException is not being caught in either case, so I can't see why it's having such an impact.
So again, with the return in the catch block it doesn't work, without it it does... I really can't see why this would happen.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我的第一个想法是,
catch
块仅在抛出IOException
时才会产生影响 - 所以我们确信这种情况正在发生。同样,我们知道如果没有 catch 块,此异常将从wfsGetCapability
方法中传播出来(大概您在删除 catch 块时添加了适当的throws
声明)。因此,在“工作”情况下,调用代码可能会捕获更高层的 IOException 并以某种方式处理它,使您的方法根据输出判断似乎可以工作。当然,您的方法不起作用,并引发了异常,但也许有一个在错误情况下调用的默认后备(或类似)?
这就是希望能够解决的“奇怪”部分。至于“问题”部分,如果您在使用 catch 块时抛出 NullPointerException,但没有它,则很明显您的 catch 块逻辑不太正确并导致了此问题。从您发布的内容来看,这些直接引用中的任何一个都不会为空 - 查看堆栈跟踪并确定从哪一行抛出异常,然后像修复任何其他 NPE 一样修复它。
Well, my first thought is that the
catch
block can only make a difference if anIOException
is thrown - so we know for sure that that's happening. And likewise we know that without the catch block, this exception will propagate out of thewfsGetCapabilities
method (presumably you added an appropriatethrows
declaration when you removed the catch block).So chances are, in the "working" case, the calling code is catching the IOException higher up and handling it in such a way that your method appears to work judging by the output. Of course your method didn't work, and threw an exception, but perhaps there's a default fallback (or similar) which is invoked in an error case?
So that's the "strange" part hopefully dealt with. As for the "problem" part, if you're getting a
NullPointerException
thrown with the catch block, but not without it, it's clear that your catch block logic is not quite right and is causing this problem. From what you've posted it doesn't look like any of those immediate references would be null - take a look at the stack trace and determine from which line the exception is thrown, then fix it as you would any other NPE.