我使用的try/catch塊有一個奇怪的問題。我有這種方法,它只是從遠程服務獲取一些數據並存儲它。在Java的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塊的返回它不起作用,沒有它......我真的不明白爲什麼會發生這種情況。
任何想法?
你要從哪裏訪問數據? (當它爲空時) – RMT
顯示堆棧跟蹤。 –
定義「它有效」....這可能意味着您返回了某種對象,您沒有記錄異常,或者您有一些業務案例,此方法處理完美。 –