有时当我打开一个流时:
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 吗?
谢谢
编辑(附加代码;-)
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)