0
有時,當我打開一個流:IOException異常停止應用雖然異常被處理
stream = url.openStream();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(stream));
我得到以下異常:
java.io.IOException:
Server returned HTTP response code: 500 for URL: https://...
和我的應用程序結束,雖然我在處理與例外:
catch (IOException ioe) {
ioe.printStackTrace();
}
有人請向我解釋爲什麼catch塊沒有進入,雖然我這是一個IOEcxeption?
THX
編輯(額外的代碼;-)
private String request(URL url) {
InputStream stream = null;
String line, response = new String();
try {
stream = url.openStream();
BufferedReader buffReader =
new BufferedReader(new InputStreamReader(stream));
while ((line = buffReader.readLine()) != null)
response += line;
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (HTTPException httpexc) {
httpexc.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return response;
}
我在我的應用程序經常運行這段代碼,只是有的時候我提到的例外。
這裏的堆棧跟蹤的一部分:
java.io.IOException: Server returned HTTP response code: 500 for URL: ...
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
你能告訴我們你是如何處理它的嗎?包括try塊。 :) – PermGenError 2013-03-25 10:44:02
你能發佈整個代碼嗎? – LaGrandMere 2013-03-25 10:44:14
很可能它被抓到別的地方。例如在另一個線程或嵌套塊中。 – 2013-03-25 10:44:16